Using Crypto++ static library in a QT project

有些话、适合烂在心里 提交于 2019-12-04 19:35:25
jww

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.

I can comment on this warning. You should perform the steps it says:

mv config.recommend config.h

config.recommend puts the library is a better configuration by completely avoiding known undefined behavior that could not be removed without breaking versioning. Since you don't appear to have versioning issues (like say, Fedora or Debian), then you can perform the move.


I now link this in my QT project file as

TEMPLATE = app

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

CONFIG += console c++11
...

When you build Crypto++, you should use the same compiler and flags for the library and app. I suggest the following.

Crypto++:

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

Qt App

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

Also see GNUmakefile | Building the Library on the 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*)'
...

These are coming from source (*.cpp) files. I'm guessing (and its purely a guess) one of two problems:

  • C++03 vs C++11 is causing missing symbols
  • QT Creator is not using libcryptopp.a

Use nm to inspect the symbols. Something like the following (the ' T " tells you its defined and in the text section):

$ 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)

If the symbols are present by QT Creator is not finding the Crypto++ library, then see something like Adding external library into Qt Creator project.


From Comments:

-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.

An archive, like libcryptopp.a, is a collection of object files. You add it to OBJECTS, not LIBS, so you want something like:

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

You use -L to specify a library path to a linker. It does not make much sense to -L/usr/lib/libcryptopp.a since its used for paths.


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 ?

On Linux, you can force static linking by either (1) -Bstatic -lcryptopp; or (2) directly specifying /usr/lib/libcryptopp.a. The Crypto++ test program uses method (2):

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

On OS X, the linker always links to the dynamic object. It even does so on iOS, where userland is usually not allowed to load dynamic objects. To avoid dynamic linking, either (1) move or rename the *.dylib; or (2) directly specifying /usr/lib/libcryptopp.a. The Crypto++ test program uses method (2):

g++ main.cpp /usr/lib/libcryptopp.a -o main.exe
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!