由于引用未定义,无法链接Crypto ++ Speck算法

时间:2019-04-05 01:20:11

标签: c++ linker-errors crypto++

我正在尝试将Crypto ++的Simon和Speck实现用于一个研究项目,但是我目前无法编译和链接其Wiki页面上提供的示例代码。

按照Crypto ++ Wiki的指示,我能够构建该库并运行其所有测试套件以完成操作。我还能够从同一Wiki运行示例AES代码。仅当我尝试调用Simon或Speck时,才会出现编译错误。

要编译和链接我正在使用的文件g++ speck_test.cpp -lcryptopp

这是Crypto ++ Speck Wiki上提供的程序。我在必要时添加了include语句和范围解析。

#include <iostream>
#include <iomanip>

#include "cryptopp/modes.h"
#include "cryptopp/speck.h"
#include "cryptopp/filters.h"

int main(int argc, char* argv[]) {
    CryptoPP::SPECK128::Encryption speck;

    std::cout << "StaticAlgorithmName: " << speck.StaticAlgorithmName() << std::endl;
    std::cout << "AlgorithmName (unkeyed): " << speck.AlgorithmName() << std::endl;

    CryptoPP::byte key[CryptoPP::SPECK128::DEFAULT_KEYLENGTH] = {};
    speck.SetKey(key, sizeof(key));

    std::cout << "AlgorithmName (keyed): " << speck.AlgorithmName() << std::endl;

    return 0;
}

根据Wiki,该程序的预期输出为

StaticAlgorithmName: SPECK-128
AlgorithmName (unkeyed): SPECK-128
AlgorithmName (keyed): SPECK-128(128)

但是,我从编译器收到以下错误:

/tmp/ccD02cM7.o: In function `CryptoPP::SPECK128::Base::Base()':
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1284BaseC2Ev[_ZN8CryptoPP8SPECK1284BaseC5Ev]+0x2c): undefined reference to `vtable for CryptoPP::SPECK128::Base'
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1284BaseC2Ev[_ZN8CryptoPP8SPECK1284BaseC5Ev]+0x3a): undefined reference to `vtable for CryptoPP::SPECK128::Base'
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1284BaseC2Ev[_ZN8CryptoPP8SPECK1284BaseC5Ev]+0x49): undefined reference to `vtable for CryptoPP::SPECK128::Base'
/tmp/ccD02cM7.o: In function `CryptoPP::SPECK128::Base::~Base()':
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1284BaseD2Ev[_ZN8CryptoPP8SPECK1284BaseD5Ev]+0xf): undefined reference to `vtable for CryptoPP::SPECK128::Base'
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1284BaseD2Ev[_ZN8CryptoPP8SPECK1284BaseD5Ev]+0x1d): undefined reference to `vtable for CryptoPP::SPECK128::Base'
/tmp/ccD02cM7.o:speck_test.cpp:(.text._ZN8CryptoPP8SPECK1284BaseD2Ev[_ZN8CryptoPP8SPECK1284BaseD5Ev]+0x2c): more undefined references to `vtable for CryptoPP::SPECK128::Base' follow
/tmp/ccD02cM7.o: In function `CryptoPP::SPECK128::Enc::Enc()':
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1283EncC2Ev[_ZN8CryptoPP8SPECK1283EncC5Ev]+0x1b): undefined reference to `vtable for CryptoPP::SPECK128::Enc'
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1283EncC2Ev[_ZN8CryptoPP8SPECK1283EncC5Ev]+0x29): undefined reference to `vtable for CryptoPP::SPECK128::Enc'
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1283EncC2Ev[_ZN8CryptoPP8SPECK1283EncC5Ev]+0x38): undefined reference to `vtable for CryptoPP::SPECK128::Enc'
/tmp/ccD02cM7.o: In function `CryptoPP::SPECK128::Enc::~Enc()':
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1283EncD2Ev[_ZN8CryptoPP8SPECK1283EncD5Ev]+0xf): undefined reference to `vtable for CryptoPP::SPECK128::Enc'
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1283EncD2Ev[_ZN8CryptoPP8SPECK1283EncD5Ev]+0x1d): undefined reference to `vtable for CryptoPP::SPECK128::Enc'
/tmp/ccD02cM7.o:speck_test.cpp:(.text._ZN8CryptoPP8SPECK1283EncD2Ev[_ZN8CryptoPP8SPECK1283EncD5Ev]+0x2c): more undefined references to `vtable for CryptoPP::SPECK128::Enc' follow
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE[_ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE]+0x28): undefined reference to `CryptoPP::SPECK128::Base::AlgorithmProvider[abi:cxx11]() const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE[_ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE]+0x30): undefined reference to `CryptoPP::SPECK128::Base::UncheckedSetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&)'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE[_ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE]+0x38): undefined reference to `CryptoPP::SPECK128::Enc::ProcessAndXorBlock(unsigned char const*, unsigned char const*, unsigned char*) const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE[_ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE]+0x40): undefined reference to `CryptoPP::SPECK128::Enc::AdvancedProcessBlocks(unsigned char const*, unsigned char const*, unsigned char*, unsigned long, unsigned int) const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE[_ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE]+0xe0): undefined reference to `non-virtual thunk to CryptoPP::SPECK128::Base::UncheckedSetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&)'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE[_ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE]+0x128): undefined reference to `non-virtual thunk to CryptoPP::SPECK128::Base::AlgorithmProvider[abi:cxx11]() const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE[_ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE]+0x130): undefined reference to `non-virtual thunk to CryptoPP::SPECK128::Enc::ProcessAndXorBlock(unsigned char const*, unsigned char const*, unsigned char*) const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE[_ZTVN8CryptoPP16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEE]+0x160): undefined reference to `non-virtual thunk to CryptoPP::SPECK128::Enc::AdvancedProcessBlocks(unsigned char const*, unsigned char const*, unsigned char*, unsigned long, unsigned int) const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE[_ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE]+0x28): undefined reference to `CryptoPP::SPECK128::Base::AlgorithmProvider[abi:cxx11]() const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE[_ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE]+0x30): undefined reference to `CryptoPP::SPECK128::Base::UncheckedSetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&)'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE[_ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE]+0x38): undefined reference to `CryptoPP::SPECK128::Enc::ProcessAndXorBlock(unsigned char const*, unsigned char const*, unsigned char*) const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE[_ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE]+0x40): undefined reference to `CryptoPP::SPECK128::Enc::AdvancedProcessBlocks(unsigned char const*, unsigned char const*, unsigned char*, unsigned long, unsigned int) const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE[_ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE]+0xd8): undefined reference to `non-virtual thunk to CryptoPP::SPECK128::Base::UncheckedSetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&)'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE[_ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE]+0x120): undefined reference to `non-virtual thunk to CryptoPP::SPECK128::Base::AlgorithmProvider[abi:cxx11]() const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE[_ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE]+0x128): undefined reference to `non-virtual thunk to CryptoPP::SPECK128::Enc::ProcessAndXorBlock(unsigned char const*, unsigned char const*, unsigned char*) const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE[_ZTVN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE]+0x158): undefined reference to `non-virtual thunk to CryptoPP::SPECK128::Enc::AdvancedProcessBlocks(unsigned char const*, unsigned char const*, unsigned char*, unsigned long, unsigned int) const'
/tmp/ccD02cM7.o:(.data.rel.ro._ZTIN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE[_ZTIN8CryptoPP12ClonableImplINS_16BlockCipherFinalILNS_9CipherDirE0ENS_8SPECK1283EncEEES4_EE]+0x10): undefined reference to `typeinfo for CryptoPP::SPECK128::Enc'
/tmp/ccD02cM7.o: In function `CryptoPP::SPECK128::Base::Base(CryptoPP::SPECK128::Base const&)':
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1284BaseC2ERKS1_[_ZN8CryptoPP8SPECK1284BaseC5ERKS1_]+0x41): undefined reference to `vtable for CryptoPP::SPECK128::Base'
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1284BaseC2ERKS1_[_ZN8CryptoPP8SPECK1284BaseC5ERKS1_]+0x4f): undefined reference to `vtable for CryptoPP::SPECK128::Base'
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1284BaseC2ERKS1_[_ZN8CryptoPP8SPECK1284BaseC5ERKS1_]+0x5e): undefined reference to `vtable for CryptoPP::SPECK128::Base'
/tmp/ccD02cM7.o: In function `CryptoPP::SPECK128::Enc::Enc(CryptoPP::SPECK128::Enc const&)':
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1283EncC2ERKS1_[_ZN8CryptoPP8SPECK1283EncC5ERKS1_]+0x26): undefined reference to `vtable for CryptoPP::SPECK128::Enc'
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1283EncC2ERKS1_[_ZN8CryptoPP8SPECK1283EncC5ERKS1_]+0x34): undefined reference to `vtable for CryptoPP::SPECK128::Enc'
speck_test.cpp:(.text._ZN8CryptoPP8SPECK1283EncC2ERKS1_[_ZN8CryptoPP8SPECK1283EncC5ERKS1_]+0x43): undefined reference to `vtable for CryptoPP::SPECK128::Enc'
collect2: error: ld returned 1 exit status

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

正如戴夫指出的那样,该错误是由于编译器无法找到CryptoPP而引起的。

要解决此问题,我必须在Ubuntu上设置LD_LIBRARY_PATH环境变量以包含库的路径。然后,我不得不更改编译器命令以先编译我的文件,然后再链接库,即

g++ -o test.exe speck_test.cpp -L<path-to-cryptopp> -lcryptopp

而不是

g++ -L<path-to-cryptopp> -lcryptopp -o test.exe speck_test.cpp,失败了。