【问题标题】:CLion and Crypto++ libraryCLion 和 Crypto++ 库
【发布时间】:2017-11-06 23:41:54
【问题描述】:

前段时间我开始在 Visual Studio 2015 中编写我的应用程序,设置所有库依赖项都没有问题。

现在,我决定搬到 CLion。但是,我的应用程序依赖于 cryptopp 库,我需要将其链接到我的 CLion 项目中。

目前,我面临大量 undefined reference 错误

undefined reference to `CryptoPP::Integer::Integer(char const*)'
undefined reference to `CryptoPP::Integer::Integer(char const*)'
undefined reference to `CryptoPP::Integer::Integer(char const*)'
undefined reference to `CryptoPP::DH_Domain<CryptoPP::DL_GroupParameters_GFP_DefaultSafePrime, CryptoPP::EnumToType<CryptoPP::CofactorMultiplicationOption, 0> >::AccessGroupParameters()'
undefined reference to `CryptoPP::DH_Domain<CryptoPP::DL_GroupParameters_GFP_DefaultSafePrime, CryptoPP::EnumToType<CryptoPP::CofactorMultiplicationOption, 0> >::GetGroupParameters() const'
undefined reference to `CryptoPP::DH_Domain<CryptoPP::DL_GroupParameters_GFP_DefaultSafePrime, CryptoPP::EnumToType<CryptoPP::CofactorMultiplicationOption, 0> >::GetGroupParameters() const'
[..]

我确实在我的 CMakeLists 中设置了包含目录:

set(EXTERN_LIBS E:/dev/libs)

include_directories(${EXTERN_LIBS} ${EXTERN_LIBS}/include)
link_directories(${EXTERN_LIBS})

但是,我仍然无法让它工作。

我在我的项目中使用 MinGW。这是设置和版本的预览:

如何在 CLion 中将cryptopp 库正确添加到我的项目中?

【问题讨论】:

  • 使用link_directories(${EXTERN_LIBS}),您只需将目录添加到搜索路径。您实际上并没有告诉 CMake link 与任何库。为此,请使用 target_link_libraries 命令。
  • @Someprogrammerdude 我已经尝试过了。使用target_link_libraries(myapp cryptlib.lib) 时,我得到mingw32/bin/ld.exe: cannot find -lcryptlib 错误。当我指定库的完整路径时,此错误消失了,但 undefined reference 错误仍然存​​在。
  • 您的 cryptlib.lib 是使用 MS VC++ 构建的。您现在正试图将它与使用 GCC (MinGW) 编译的目标文件链接起来。那是行不通的。 GCC 和 VC++ 有不兼容的ABIs,特别是不同的name mangling 协议。因此,MinGW 在您的目标代码中发出的损坏的 C++ 标识符与您的 VC++ 编译库导出的任何标识符都不匹配,并且是未定义的引用。您需要使用 MinGW 从源代码构建 cryptopp 以制作与 ABI 兼容的库。
  • @MikeKinghan 那我可以使用 Visual Studio 编译器吗?我读过一些关于cryptopp的文章,结果发现它不支持CMake。
  • 真的吗? cryptopp 5.6.5 GitHub download 中的 CMakeLists.txt 在 Windows 10 Pro 上使用 mingw-w64 GCC 7.2.0 构建软件包时没有问题。当然,您可以使用 MS VC++ 构建它

标签: c++ mingw static-libraries clion crypto++


【解决方案1】:

我想我们可能已经在Commit e4cef84883b2 解决了 MinGW/C++11 问题。您应该从 Master 工作或执行 git pull,然后取消注释 CRYPTOPP_NO_CXX11config.h : 65 中的定义(左右):

// Define CRYPTOPP_NO_CXX11 to avoid C++11 related features shown at the
// end of this file. Some compilers and standard C++ headers advertise C++11
// but they are really just C++03 with some additional C++11 headers and
// non-conforming classes. You might also consider `-std=c++03` or
// `-std=gnu++03`, but they are required options when building the library
// and all programs. CRYPTOPP_NO_CXX11 is probably easier to manage but it may
// cause -Wterminate warnings under GCC. MSVC++ has a similar warning.
// Also see https://github.com/weidai11/cryptopp/issues/529
// #define CRYPTOPP_NO_CXX11 1

我认为问题在于,您遇到了与 Windows 相关的问题,以及它缺乏适当的 C++11 支持,但您间接地得到了这些问题。它们是间接的,因为 MinGW 和 GCC 是分层的。 MinGW 和 GCC 不可能提供 C++11,因为底层平台不能。

我认为此时最好的选择是定义CRYPTOPP_NO_CXX11。我不相信我们可以像在 Windows 上那样为您做到这一点,因为我们需要访问的定义隐藏在 MinGW 和 GCC 后面。我们还需要解决一些 MSVC++ 错误。

这是我们在 Windows 上的操作方式,但我们无法访问 MinGW 中的这些定义(来自 config.h : 950):

// Dynamic Initialization and Destruction with Concurrency ("Magic Statics")
// MS at VS2015 with Vista (19.00); GCC at 4.3; LLVM Clang at 2.9; Apple Clang at 4.0; Intel 11.1; SunCC 5.13.
// Microsoft's implementation only works for Vista and above, so its further
// limited. http://connect.microsoft.com/VisualStudio/feedback/details/1789709
#if (CRYPTOPP_MSC_VERSION >= 1900) && ((WINVER >= 0x0600) || (_WIN32_WINNT >= 0x0600)) || \
    (CRYPTOPP_LLVM_CLANG_VERSION >= 20900) || (CRYPTOPP_APPLE_CLANG_VERSION >= 40000) || \
    (__INTEL_COMPILER >= 1110) || (CRYPTOPP_GCC_VERSION >= 40300) || (__SUNPRO_CC >= 0x5130)
# define CRYPTOPP_CXX11_DYNAMIC_INIT 1
#endif // Dynamic Initialization compilers

如果您定义了CRYPTOPP_NO_CXX11,那么将定义以下问题,您将避免这些问题:CRYPTOPP_CXX11_DYNAMIC_INITCRYPTOPP_CXX11_SYNCHRONIZATIONCRYPTOPP_CXX11_ATOMICS


第二个问题,与Clion和Cmake有关的问题,解决如下。我们使用 Autotools 和 Cmake 文件设置了一个单独的 GitHub。 Autotools 文件位于cryptopp-autotools,Cmake 文件位于cryptopp-cmake

存储库在我的 GiHub 中,因为这是编写库并提供 Crypto++ GitHub 的 Wei Dai 喜欢避免的那种管理。逻辑分离还有助于建立逻辑边界,因此人们知道 Autotools 和 CMake 不是 Crypto++ 官方发行版的一部分。

社区负责 Autotools 和 Cmake,我们将与社区一起解决问题。如果社区投入工作,那么 Autotools 和 Cmake 将会改进。如果 Autotools 或 CMake 变得稳定,那么我们会将包含文件的 tarball 添加到官方发行版中。

目前 Autotools 和 Cmake 处于需要改进的状态。 Cmake 的问题在 wiki 上的CMake | Current Status 中有详细说明。 Autotools 的问题并没有真正记录下来,因为我与发行版维护者一起工作。有点像我们知道问题是什么,但大多数其他人不知道。

【讨论】:

  • 很高兴我可以帮助改进 cmake 的 crypto++。很好的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多