对CryptoPP::AlignedAllocate(unsigned int)的未定义引用

Undefined reference to CryptoPP::AlignedAllocate(unsigned int)

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

我在c++linux中使用crypto++。这是我的简单代码:

#include <iostream>
#include <fstream>
#include <string.h>
#include "crypto++/cryptlib.h"
#include "crypto++/modes.h"
#include "crypto++/filters.h"
#include "crypto++/aes.h"
#include "crypto++/osrng.h"
#include "crypto++/strciphr.h"
using namespace std;
using namespace CryptoPP;
ifstream::pos_type size;
char * memblock;
int length;
char * _iv[AES::BLOCKSIZE];
char * keys[AES::MAX_KEYLENGTH];

void encriptCTR(byte * outbyte, const byte * inbyte, const byte * key, const byte * iv);
void encriptCTR(byte * outbyte, const byte * inbyte, const byte * key, const byte * iv)
{
    size_t inbyte_len = strlen((const char *)inbyte);
    CTR_Mode<AES>::Encryption ctr_encription(key, strlen((const char*)key), iv);
    ctr_encription.ProcessData(outbyte, inbyte, inbyte_len);
}
int main()
{
    ifstream file;
    file.open("testaja", ios::binary);
    if (file.is_open())
    {
        file.seekg (0, ios::end);
        length = file.tellg();
        memblock = new char [length];
        file.seekg (0, ios::beg);
        file.read (memblock, length);

        if (!file)
        {
            int a;
            a = (int)file.gcount();
            file.clear();
        }
        else
        {
            file.close();
            for (int i = 0; i < length; ++i)
            {
                cout << hex << (int)memblock[i] << " ";
            }
        }
    }
}

当我运行它时,出现了一些错误:

 undefined reference to `CryptoPP::AlignedAllocate(unsigned int)'
 undefined reference to `CryptoPP::UnalignedAllocate(unsigned int)'
 undefined reference to `CryptoPP::AlignedDeallocate(unsigned int)'
 undefined reference to `CryptoPP::UnalignedDeallocate(unsigned int)'

然后,我使用命令

gcc -o test test.cpp -L/usr/lib/crypto++ -lcrypto++

但这个错误仍然存在:

undefined reference to `CryptoPP::AlignedAllocate(unsigned int)'
undefined reference to `CryptoPP::UnalignedAllocate(unsigned int)'
undefined reference to `CryptoPP::AlignedDeallocate(unsigned int)'
undefined reference to `CryptoPP::UnalignedDeallocate(unsigned int)'

如何修复此错误?我的代码有问题吗?

我正在使用突触包管理器为这个包安装crypto++:

libcrypto++-utils
libcrypto++8
libcrypto++8-dbg
libcrypto++-dev
libcrypto++-doc

libcrypto++.a和libcrypto++.so可以在/usr/lib/中找到

提前谢谢。

此命令看起来错误:

gcc -o test test.cpp -L/usr/lib/crypto++ -lcrypto++

如果(正如你所说)库在/usr/lib中,那么你不应该说-L/usr/lib/crypto++

我认为libcrypto++8包将其库安装在-L/usr/lib/crypto++目录中,可能它们是不兼容的,并且没有提供程序所需的未定义符号。

您应该使用以下代码进行编译:

gcc -o test test.cpp -lcrypto++

(不需要说-L/usr/lib,因为它是库的默认位置)

它解决了!我将命令更改为:

g++ -o test test.cpp -L/usr/lib/crypto++ -lcrypto++

到该命令:

g++ -o test test.cpp -L/usr/lib/ -lcryptopp -lpthread

我添加-lpthread是因为在我使用以下命令之后:

g++ -o test test.cpp -L/usr/lib/ -lcryptopp

我得到这些错误:

./libcryptopp.so: undefined reference to `pthread_getspecific'
./libcryptopp.so: undefined reference to `pthread_key_delete'
./libcryptopp.so: undefined reference to `pthread_key_create'
./libcryptopp.so: undefined reference to `pthread_setspecific'

我误解了-L/usr/lib/crypto++arg,我以为编译器会在/usr/lib/dir中搜索crypto++,结果编译器会在-L/usr/lib/corypto++目录中搜索crupto++,而安装在-L/usr/lib/dir中的包。

感谢@jonathan wakely。

我也有这个问题。你的编译器需要将库文件绑定到你的程序,因为它找不到你的decation的任何实现!

我仍然没有解决我的问题。但你还有别的办法!!!您可以将.cpp原始文件替换为库文件。

您可以从以下链接下载Cryptopp

https://www.cryptopp.com/cryptopp563.zip

相关文章: