【发布时间】: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++