链接时对CryptoPP::AlignedAllocate的未定义引用
Undefined reference to CryptoPP::AlignedAllocate when linking
我在Windows中编译cryptopp项目时遇到以下错误
C:UsersSajithAppDataLocalTempccxq8O8x.o:aescbc.cpp:(.text$_ZN8CryptoPP20AllocatorWithCleanupIhLb1EE8allocateEjPKv[
__ZN8CryptoPP20AllocatorWithCleanupIhLb1EE8allocateEjPKv]+0x2e): undefined reference to `CryptoPP::AlignedAllocate(unsig
ned int)'
C:UsersSajithAppDataLocalTempccxq8O8x.o:aescbc.cpp:(.text$_ZN8CryptoPP20AllocatorWithCleanupIhLb1EE10deallocateEPv
j[__ZN8CryptoPP20AllocatorWithCleanupIhLb1EE10deallocateEPvj]+0x28): undefined reference to `CryptoPP::AlignedDeallocate
(void*)'
collect2.exe: error: ld returned 1 exit status
下面是我的编译命令:
mingw32-g++.exe -o .aestest2.exe .aescbc.cpp -I "C:cryptoppInclude" -L "C:cryptoppLib" -lcryptopp
我的libcryptopp.a位于C:cryptoppLib
我试图找出AlignedDeallocate
在哪里声明,但我做不到。
引发此错误的程序部分如下:
try
{
cout << "plain text: " << plain << endl;
CBC_Mode< AES >::Encryption e;
e.SetKeyWithIV(key, sizeof(key), iv);
// The StreamTransformationFilter removes
// padding as required.
StringSource s(plain, true,
new StreamTransformationFilter(e,
new StringSink(cipher)
) // StreamTransformationFilter
); // StringSource
#if 0
StreamTransformationFilter filter(e);
filter.Put((const byte*)plain.data(), plain.size());
filter.MessageEnd();
const size_t ret = filter.MaxRetrievable();
cipher.resize(ret);
filter.Get((byte*)cipher.data(), cipher.size());
#endif
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
exit(1);
}
建议不胜感激!
AlignedAllocate
在misc.h
:中
$ grep -I AlignedAllocate *
misc.cpp:void * AlignedAllocate(size_t size)
misc.h:CRYPTOPP_DLL void * CRYPTOPP_API AlignedAllocate(size_t size);
secblock.h: return (pointer)AlignedAllocate(n*sizeof(T));
和:
$ grep -R AlignedDeallocate *
misc.cpp:void AlignedDeallocate(void *p)
misc.h:CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *p);
secblock.h: return AlignedDeallocate(p);
然而,他们受到保护:
#if CRYPTOPP_BOOL_ALIGN16_ENABLED
CRYPTOPP_DLL void * CRYPTOPP_API AlignedAllocate(size_t size);
CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *p);
#endif
CRYPTOPP_BOOL_ALIGN16_ENABLED
设置在config.h
:中
#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE || CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE)
#define CRYPTOPP_BOOL_ALIGN16_ENABLED 1
#else
#define CRYPTOPP_BOOL_ALIGN16_ENABLED 0
#endif
在Crypto++内部做出选择后,您可以考虑将以下内容添加到config.h
中:
#undef CRYPTOPP_BOOL_ALIGN16_ENABLED
#define CRYPTOPP_BOOL_ALIGN16_ENABLED 1
然后,重建库和程序。
可能发生的其他情况是:MinGW正在禁用MMX/SSE/SSE2的计算机上构建库。或者他们可能正在使用g++ -mno-sse -mno-sse2 ...
。
然后,您将获得一个闪亮的新英特尔或AMD,并且基于g++启用的内容和config.h
中的定义,您的程序期望AlignedAllocate
和AlignedDeallocate
,因为您的配置包括MMX/SSE/SSE2…
这种情况在config.h| Crypto++wiki上的建议中进行了讨论。这就是为什么我们告诉发行版启用和禁用config.h
中的东西,而不是从命令行。修改config.h
可以确保发行版和用户程序大多使用相同的设置。
如果是这种情况,那么您可以尝试:
export CXXFLAGS="-DNDEBUG -g2 -O2 -mno-sse -mno-sse2"
mingw32-g++.exe $CXXFLAGS -o .aestest2.exe .aescbc.cpp
-I "C:cryptoppInclude" -L "C:cryptoppLib" -lcryptopp
有大量基于MMX/SSE/SSE2的级联定义;请参阅wiki上的config.h|程序集定义。由于MMX/SSE/SSE2被禁用,您将获得AES的纯软件实现。它不会表现得那么好。
-lcryptopp
之前添加-static
。希望这对你有用编辑:实际上,别管我的第一个答案。MSYS2的repo中可能有一个损坏的共享库版本,但我想你自己编译了它。
相反,我将提供一种如何使用MinGW编译Crypto++的方法。您需要Qt的qmake来生成Makefile,而不是使用包含的Makefile。
首先,打开GNUMakefile并查找TESTOBJS
,从5.6.2开始,它就是bench.o bench2.o test.o validat1.o validat2.o validat3.o adhoc.o datatest.o regtest.o fipsalgt.o dlltest.o
。您需要删除具有相同名称的.cpp,这样就不会构建臃肿的lib。
然后打开cmd.exe(PATH
包含MinGW的/bin和qmake.exe所在的目录),导航到包含源的目录并使用这些:
erase /f GNUmakefile
qmake -project
它将创建一个具有当前文件夹名称和扩展名".pro"的文件。用文本编辑器打开它,更改
TEMPLATE = app
TARGET = cryptopp562
INCLUDEPATH += .
至
TEMPLATE = lib
TARGET = cryptopp
INCLUDEPATH += .
CONFIG -= qt
CONFIG += static
LIBS += -lws2_32
QMAKE_CXXFLAGS_RELEASE += -DNDEBUG
将fipstest.cpp中的每个OutputDebugString
替换为OutputDebugStringA
现在您可以键入qmake
来生成一个新的Makefile并使用mingw32-make
。将会有大量的警告,主要是关于未使用的参数。尽管如此,没有什么严重到可以取消汇编的地步。libcryptopp.a
将在./release/
中。
问题中的示例编译并与以这种方式编译的库完美配合,使用与Qt捆绑的MinGW 4.9.2和独立的MingGW-w64 5.2.0进行测试。
基本的想法是从这里来的,我只是改进了它。
如果您在程序中使用Crypto++
,请注意:
此解决方案适用于linux,如果您执行以下命令,您的问题100%将解决
如果你想链接你的.so
文件并最终编译你的程序,首先你必须在ubuntu或其他发行版(debian-base-linux)中安装来自Synaptic工具的所有Crypto++
库。
之后,您在qt创建者中将-lcryptopp
或/和-lcrypto++
添加到您的.pro
文件中,如下所示:
LIBS += -lcryptopp
LIBS += -lcrypto++
我有这个问题,但我的问题解决了。。。
我的代码没有任何错误
如果需要,可以在windows中进行测试,并在windows中的qtcreator中添加两条top语句。
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用