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

sjs*_*sam 2 c++ mingw crypto++

我在Windows中编译cryptopp项目时遇到以下错误。

C:\Users\Sajith\AppData\Local\Temp\ccxq8O8x.o:aescbc.cpp:(.text$_ZN8CryptoPP20AllocatorWithCleanupIhLb1EE8allocateEjPKv[
__ZN8CryptoPP20AllocatorWithCleanupIhLb1EE8allocateEjPKv]+0x2e): undefined reference to `CryptoPP::AlignedAllocate(unsig
ned int)'
C:\Users\Sajith\AppData\Local\Temp\ccxq8O8x.o:aescbc.cpp:(.text$_ZN8CryptoPP20AllocatorWithCleanupIhLb1EE10deallocateEPv
j[__ZN8CryptoPP20AllocatorWithCleanupIhLb1EE10deallocateEPvj]+0x28): undefined reference to `CryptoPP::AlignedDeallocate
(void*)'
collect2.exe: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)


下面是我的编译命令:

mingw32-g++.exe -o .\aestest2.exe .\aescbc.cpp   -I "C:\cryptopp\Include" -L "C:\cryptopp\Lib" -lcryptopp
Run Code Online (Sandbox Code Playgroud)


我的libcryptopp.a位于C:\cryptopp\Lib
我试图找出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);
    }
Run Code Online (Sandbox Code Playgroud)


建议表示赞赏!

jww*_*jww 5

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));
Run Code Online (Sandbox Code Playgroud)

和:

$ grep -R AlignedDeallocate *
misc.cpp:void AlignedDeallocate(void *p)
misc.h:CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *p);
secblock.h:                     return AlignedDeallocate(p);
Run Code Online (Sandbox Code Playgroud)

但是,他们要注意:

#if CRYPTOPP_BOOL_ALIGN16_ENABLED
CRYPTOPP_DLL void * CRYPTOPP_API AlignedAllocate(size_t size);
CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *p);
#endif
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

您可以考虑config.h在Crypto ++内部选择之后添加以下内容:

#undef CRYPTOPP_BOOL_ALIGN16_ENABLED
#define CRYPTOPP_BOOL_ALIGN16_ENABLED 1
Run Code Online (Sandbox Code Playgroud)

然后,重建库和您的程序。


可能正在发生的其他事情是:MinGW正在禁用MMX / SSE / SSE2的计算机上构建库。也许他们正在使用g++ -mno-sse -mno-sse2 ...

然后,您便有了一个闪亮的新Intel或AMD,并基于g ++启用的功能和中的定义config.h,您的程序可以期望AlignedAllocate并且AlignedDeallocate因为您的配置包括MMX / SSE / SSE2 ...

config.h中讨论了这种情况建议在加密+维基。这就是为什么我们告诉发行版启用和禁用in中的内容config.h,而不是从命令行中启用它的原因。进行修改config.h可确保发行版和用户程序大多使用相同的设置。

如果是这种情况,那么您可以尝试:

export CXXFLAGS="-DNDEBUG -g2 -O2 -mno-sse -mno-sse2"
mingw32-g++.exe $CXXFLAGS -o .\aestest2.exe .\aescbc.cpp \
  -I "C:\cryptopp\Include" -L "C:\cryptopp\Lib" -lcryptopp
Run Code Online (Sandbox Code Playgroud)

有许多基于MMX / SSE / SSE2的层叠定义。参见config.h | 程序集在Wiki上定义。由于禁用了MMX / SSE / SSE2,因此您将获得AES的纯软件实现。它的性能可能不尽如人意。