在QT项目中使用Crypto ++静态库 [英] Using Crypto++ static library in a QT project

查看:215
本文介绍了在QT项目中使用Crypto ++静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在系统上静态构建了cryptopp,它也通过了所有测试.这些是我在测试过程中得到的警告

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.

我现在将其链接为

TEMPLATE = app

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

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

SOURCES += main.cpp \
hashdata.cpp

HEADERS += \
hashdata.hpp

但是当我编译它时,我得到了所有未定义的错误.

But when I compile this I get all undefined errors.

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 seen a similar problem previously when I searched in google, but the solution isn't clear. Could it be because of C++11 flags ?

推荐答案

我已经在系统上静态构建了cryptopp,它也通过了所有测试. 这些是我在测试过程中得到的警告

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

警告:config.h中未定义CRYPTOPP_NO_UNALIGNED_DATA_ACCESS. 警告:CRYPTOPP_INIT_PRIORITY未在config.h中定义.警告: CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562在config.h中定义. 警告:您应该在config.h中而不是CXXFLAGS中进行这些更改. 警告:您可以'mv config.recommend config.h',但它会中断 版本控制.警告:有关更多信息,请参见 http://cryptopp.com/wiki/config.h 详细信息.

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通过完全避免在不破坏版本控制的情况下无法删除 的已知未定义行为,从而使库成为更好的配置.由于您似乎没有版本问题(例如Fedora或Debian),因此可以执行此操作.

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.

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

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 ++时,应为库和应用程序使用相同的编译器和标志.我建议以下.

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

Crypto ++ :

Crypto++:

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

Qt应用

Qt App

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

另请参见 GNUmakefile |在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)文件.我正在猜测(并且纯粹是猜测)两个问题之一:

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

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

使用nm检查符号.类似于以下内容("T"会在文本部分告诉您其定义):

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)

如果QT Creator存在符号,则找不到Crypto ++库,然后看到类似将外部库添加到Qt Creator项目中.

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可以工作,但是我不知道为什么-L/usr/lib/libcryptopp.a不能. ...因为如果一个人同时拥有静态库和动态库,我仍然不知道如何强制链接静态库.

-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,因此您想要类似:

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

您使用-L指定链接器的库路径.由于-L/usr/lib/libcryptopp.a用于路径,因此对-L/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 ?

在Linux上,您可以通过(1)-Bstatic -lcryptopp强制执行静态链接;或(2)直接指定/usr/lib/libcryptopp.a. Crypto ++ 测试程序使用方法(2):

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

在OS X上,链接器始终链接到动态对象.它甚至在iOS上也是如此,在iOS上通常不允许userland加载动态对象.为避免动态链接,请(1)移动或重命名*.dylib;或者(2)移动或重命名*.dylib.或(2)直接指定/usr/lib/libcryptopp.a. Crypto ++ 测试程序使用方法(2):

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

这篇关于在QT项目中使用Crypto ++静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆