链接时对CryptoPP::AlignedAllocate的未定义引用

Undefined reference to CryptoPP::AlignedAllocate when linking

本文关键字:未定义 引用 AlignedAllocate 链接 CryptoPP      更新时间:2023-10-16

我在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);
    }


建议不胜感激!

AlignedAllocatemisc.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中的定义,您的程序期望AlignedAllocateAlignedDeallocate,因为您的配置包括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的纯软件实现。它不会表现得那么好。

我在使用MSYS2版本的MinGW64时也遇到了同样的情况

解决方案是在-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语句。