c++ - 在 QT 项目中使用 Crypto++ 静态库

标签 c++ linux qt crypto++

I have built cryptopp statically on my system it passes all tests too. These are the warning though I get during tests

WARNING: CRYPTOPP_NO_UNALIGNED_DATA_ACCESS is not defined in config.h.
WARNING: CRYPTOPP_INIT_PRIORITY is not defined in config.h.
WARNING: CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 is defined in config.h.
WARNING: You should make these changes in config.h, and not CXXFLAGS.
WARNING: You can 'mv config.recommend config.h', but it breaks versioning.
WARNING: See http://cryptopp.com/wiki/config.h for more details.

我现在将其链接到我的 QT 项目文件中

TEMPLATE = app

LIBS += -L/usr/lib/libcryptopp.a
#LIBS += -lcryptopp

CONFIG += console c++11
CONFIG += staticlib

SOURCES += main.cpp \
hashdata.cpp

HEADERS += \
hashdata.hpp

但是当我编译这个时,我得到了所有未定义的错误。

hashdata.o: In function `hashdata::hashfunction(std::string)':
hashdata.cpp:(.text+0x1fb): undefined reference to `CryptoPP::Algorithm::Algorithm(bool)'
hashdata.cpp:(.text+0x270): undefined reference to `CryptoPP::SHA512::InitState(unsigned long long*)'
hashdata.cpp:(.text+0x29a): undefined reference to `CryptoPP::Algorithm::Algorithm(bool)'
hashdata.cpp:(.text+0x2a1): undefined reference to `vtable for CryptoPP::StringSinkTemplate<std::string>'
hashdata.cpp:(.text+0x30b): undefined reference to `CryptoPP::Filter::Filter(CryptoPP::BufferedTransformation*)'
hashdata.cpp:(.text+0x312): undefined reference to `vtable for CryptoPP::Grouper'
hashdata.cpp:(.text+0x35e): undefined reference to `CryptoPP::Filter::Detach(CryptoPP::BufferedTransformation*)'
hashdata.cpp:(.text+0x375): undefined reference to `CryptoPP::Filter::Filter(CryptoPP::BufferedTransformation*)'
hashdata.cpp:(.text+0x37c): undefined reference to `vtable for CryptoPP::BaseN_Encoder'
hashdata.cpp:(.text+0x3d3): undefined reference to `CryptoPP::Filter::Detach(CryptoPP::BufferedTransformation*)'
hashdata.cpp:(.text+0x3e5): undefined reference to `CryptoPP::ProxyFilter::ProxyFilter(CryptoPP::BufferedTransformation*, unsigned long, unsigned long, CryptoPP::BufferedTransformation*)'
hashdata.cpp:(.text+0x3ec): undefined reference to `vtable for CryptoPP::HexEncoder'
hashdata.cpp:(.text+0x452): undefined reference to `vtable for CryptoPP::AlgorithmParametersTemplate<int>'
hashdata.cpp:(.text+0x4af): undefined reference to `vtable for CryptoPP::AlgorithmParametersTemplate<CryptoPP::ConstByteArrayParameter>'
...

之前在google上搜索的时候也看到过类似的问题,但是解决方案不太清楚。可能是因为 C++11 标志吗?

最佳答案

I have built cryptopp statically on my system it passes all tests too. These are the warning though I get during tests

WARNING: CRYPTOPP_NO_UNALIGNED_DATA_ACCESS is not defined in config.h. WARNING: CRYPTOPP_INIT_PRIORITY is not defined in config.h. WARNING: CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 is defined in config.h. WARNING: You should make these changes in config.h, and not CXXFLAGS. WARNING: You can 'mv config.recommend config.h', but it breaks versioning. WARNING: See http://cryptopp.com/wiki/config.h for more details.

我可以对此警告发表评论。您应该执行它所说的步骤:

mv config.recommend config.h

config.recommend通过完全避免在不破坏版本控制的情况下无法删除的已知未定义行为,该库是一种更好的配置。由于您似乎没有版本控制问题(例如 Fedora 或 Debian),因此您可以执行移动。


I now link this in my QT project file as

TEMPLATE = app

LIBS += -L/usr/lib/libcryptopp.a
#LIBS += -lcryptopp

CONFIG += console c++11
...

当您构建 Crypto++ 时,您应该为库和应用程序使用相同的编译器和标志。我建议如下。

加密++:

# Be sure to 'mv config.recommend config.h'
export CXXFAGS="-DNDEBUG -g2 -O3 -std=c++11"
make static dynamic test

Qt 应用程序

# main.pro file
QMAKE_CXXFLAGS += -DNDEBUG -g2 -O3

另请参阅GNUmakefile | Building the Library在 Crypto++ wiki 上。


hashdata.o: In function `hashdata::hashfunction(std::string)':
hashdata.cpp:(.text+0x1fb): undefined reference to `CryptoPP::Algorithm::Algorithm(bool)'
hashdata.cpp:(.text+0x270): undefined reference to `CryptoPP::SHA512::InitState(unsigned long long*)'
...

这些来自源 ( *.cpp ) 文件。我猜测(并且纯粹是猜测)两个问题之一:

  • C++03 与 C++11 导致符号丢失
  • QT Creator 未使用 libcryptopp.a

使用nm检查符号。类似于以下内容(“T”告诉您其定义并在文本部分中):

$ nm libcryptopp.a 2>/dev/null | c++filt | \
     grep 'Algorithm::Algorithm(bool)' | grep ' T '
0000000000000060 T CryptoPP::Algorithm::Algorithm(bool)
0000000000000070 T CryptoPP::Algorithm::Algorithm(bool)

如果 QT Creator 未找到 Crypto++ 库时出现这些符号,请查看类似 Adding external library into Qt Creator project 的内容。 .


来自评论:

-lcryptopp works, but I don't know why -L/usr/lib/libcryptopp.a doesn't. ... Because if a person had both static and dynamic libraries, I still don't know how to force linking static ones.

存档,例如 libcryptopp.a ,是目标文件的集合。您将其添加到 OBJECTS ,不是LIBS ,所以你want something like :

# main.pro file
OBJECTS += /usr/lib/libcryptopp.a

您使用-L指定链接器的库路径。 -L/usr/lib/libcryptopp.a没有多大意义因为它用于路径。


Additional note is that when both the static and dynamic libs were present it was automatically linking the dynamic lib. Do you know how to force static linking ?

在 Linux 上,您可以通过 (1) -Bstatic -lcryptopp 强制静态链接;或 (2) 直接指定/usr/lib/libcryptopp.a 。加密++ test program uses method (2) :

g++ main.cpp /usr/lib/libcryptopp.a -o main.exe

在 OS X 上,链接器始终链接到动态对象。它甚至在 iOS 上也是如此,通常不允许用户空间加载动态对象。为了避免动态链接,(1) 移动或重命名 *.dylib ;或 (2) 直接指定/usr/lib/libcryptopp.a 。加密++ test program uses method (2) :

g++ main.cpp /usr/lib/libcryptopp.a -o main.exe

关于c++ - 在 QT 项目中使用 Crypto++ 静态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38893334/

相关文章:

C++/Qt - 无法从 qrc 文件打开 .txt

linux - ping "-n"标志如何工作?并且它会影响响应时间吗? (Linux)

image - 使用OpenCV和Qt将图像加载到硬盘中

c++ - Qt:设置后如何清除setFixedSize

c++ - SDL2 库初始化失败

C++(Hashmap 风格)数据结构适合这种情况吗?

c++ - 如何删除 vector 的多个添加指针?

linux - Awk:如何跨列计算字符串的出现次数并找到跨行的最大值?

c++ - 从 C++ 中的不同项目中调用函数(在 Linux 中)

python - Qt - 格式化要在样式表中使用的 QColor?