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

9

我正在使用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)'

我该如何解决这个错误?我的代码出了什么问题吗?

我正在使用Synaptic软件包管理器安装crypto++,安装的软件包名称为:

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

感谢您的提问。

/usr/lib/ 目录下可以找到 libcrypto++.a 和 libcrypto++.so。


我尝试使用g++编译,但是那些错误仍然存在。我应该链接哪些C++代码?谢谢。 - user1533464
我相信在crypto++/secblock.h中使用了AlignedAllocate(unsigned int),其中包括了crypto++/misc.h,在该头文件中声明了AlignedAllocate(unsigned int),但是一些实现并没有找到,导致出现了这个错误。我该怎么办? - user1533464
我尝试在我的程序中包含crypto++/misc.h,但仍然出现了那些错误。 - user1533464
1
这意味着库的安装存在问题,请更新问题并提供 gcc -o test test.cpp -lcrypto++ -Wl,-v 命令的输出。(我从该命令中去掉了 -L/usr/lib/crypto++,因为如果库已经安装在 /usr/lib 中,那么告诉链接器查找不存在的目录 /usr/lib/crypto++ 是浪费时间。) - Jonathan Wakely
@Jonathan:哇!它起作用了!我将-L/usr/lib/crypto++更改为-L/usr/lib/,它就可以工作了!你是对的,我认为编译器正在寻找不存在的-L/usr/lib/crypto++目录,将其更改为-L/usr/lib/后,编译器就会寻找正确的目录,谢谢 :) - user1533464
3个回答

7

IT问题已解决!我将我的命令从以下内容更改:

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++的参数,我以为编译器会在/usr/lib/目录中搜索crypto ++,但实际上编译器会在-L/usr/lib/crypto++目录中搜索crypto ++,而软件包安装在-L/usr/lib/目录中。 感谢@jonathan wakely。

7
该命令看起来不正确:
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,因为它已经是库的默认位置了。)

0
我也有这个问题。 你的编译器需要将库文件绑定到你的程序中,因此它找不到任何声明的实现!
我仍然没有解决我的问题。但是你有另一种方法!!! 你可以用库文件代替原始的.cpp文件。
你可以从以下链接下载Cryptopp:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接