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

use*_*464 8 c++ undefined-reference crypto++

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

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,发生了一些错误:

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

然后,我用了命令

gcc -o test test.cpp -L/usr/lib/crypto++ -lcrypto++
Run Code Online (Sandbox Code Playgroud)

但是这个错误仍然存​​在:

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

我该如何解决这个错误?我的代码有问题吗?

我正在使用这个包的synaptic包管理器安装crypto ++:

libcrypto++-utils
libcrypto++8
libcrypto++8-dbg
libcrypto++-dev
libcrypto++-doc
Run Code Online (Sandbox Code Playgroud)

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

提前致谢.

Jon*_*ely 6

此命令看起来错误:

gcc -o test test.cpp -L/usr/lib/crypto++ -lcrypto++
Run Code Online (Sandbox Code Playgroud)

如果(如你所说)libs在/usr/lib那时你不应该说-L/usr/lib/crypto++

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

您应该简单地编译:

gcc -o test test.cpp -lcrypto++
Run Code Online (Sandbox Code Playgroud)

(没有必要说,-L/usr/lib因为它是图书馆的默认位置)


use*_*464 5

解决了!我将命令从:

g++ -o test test.cpp -L/usr/lib/crypto++ -lcrypto++
Run Code Online (Sandbox Code Playgroud)

此命令:

g++ -o test test.cpp -L/usr/lib/ -lcryptopp -lpthread
Run Code Online (Sandbox Code Playgroud)

我添加-lpthread,因为使用此命令后:

g++ -o test test.cpp -L/usr/lib/ -lcryptopp
Run Code Online (Sandbox Code Playgroud)

我得到这些错误:

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

我对-L / usr / lib / crypto ++ arg有误解,我认为编译器将在/ usr / lib / dir中搜索crypto ++,结果发现编译器将在-L / usr / lib / crypto ++ dir中搜索crypto ++。软件包安装在-L / usr / lib / dir中。

感谢@jonathan。