【问题标题】:Undefined reference to CryptoPP::AlignedAllocate(unsigned int)对 CryptoPP::AlignedAllocate(unsigned int) 的未定义引用
【发布时间】:2012-07-20 10:04:21
【问题描述】:

我在 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/ 中找到

提前致谢。

【问题讨论】:

  • 我尝试使用 g++ 编译,但这些错误仍然存​​在。我应该链接什么 C++ 代码?谢谢。
  • 我相信 AlignedAllocate(unsigned int) 在 crypto++/secblock.h 中使用,其中包括 crypto++/misc.h,其中声明了 AlignedAllocate(unsigned int),但不知何故 AlignedAllocate(unsigned int) 未找到实现,并且发生此错误。我该怎么办?
  • 我尝试在我的程序中包含 crypto++/misc.h,但仍然出现这些错误。
  • 这意味着库的安装方式存在问题,您能否使用gcc -o test test.cpp -lcrypto++ -Wl,-v 的输出更新问题(我从该命令中取出了-L/usr/lib/crypto++,因为如果库安装在@987654328 中@ 然后告诉链接器查看不存在的目录/usr/lib/crypto++ 是浪费时间)
  • @jonathan:哇!有用!我将 -L/usr/lib/crypto++ 更改为 -L/usr/lib/ 并且它有效!你说得对,我认为编译器会寻找不存在的 -L/usr/lib/crypto++ 目录,将其更改为 -L/usr/lib/ 后,编译器会寻找正确的目录,谢谢:)

标签: c++ undefined-reference crypto++


【解决方案1】:

这个命令看起来不对:

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,因为它是库的默认位置)

【讨论】:

    【解决方案2】:

    解决了! 我改变了我的命令:

    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/ 目录中搜索crypto++,结果编译器会在-L/usr/lib/crypto++ 目录中搜索crypto++ , 而安装在 -L/usr/lib/ 目录下的包。

    感谢@jonathan wakely。

    【讨论】:

      【解决方案3】:

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

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

      您可以从以下链接下载Cryptopp

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

      【讨论】:

        猜你喜欢
        • 2015-09-09
        • 1970-01-01
        • 2018-10-13
        • 1970-01-01
        • 1970-01-01
        • 2018-03-31
        • 1970-01-01
        • 1970-01-01
        • 2021-04-20
        相关资源
        最近更新 更多