c++ - 使用CMake构建C++项目时,链接Crypto++失败

标签 c++ cmake crypto++

我有以下项目结构:

myexec/
|-main.cpp
|-hashing.cpp
|-hashing.h
|-CMakeLists.txt
我的小软件需要Crypto++,它是通过以下路径构建的最新版本:
C:\Users\myuser\cryptopp
CMakeLists.txt是:
cmake_minimum_required (VERSION 3.8)

project ("MyExec")

add_executable(MyExec "main.cpp", "hashing.h", "hashing.cpp")

find_library(CRYPTOPP_LIB cryptopp "C:/Users/myuser/cryptopp/Win32/DLL_Output/Debug")
target_link_libraries(MyExec PUBLIC "${CRYPTOPP_LIB}")

target_include_directories(MyExec PUBLIC "C:/Users/myuser/cryptopp")
hashing.cpp是:
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 // Needed to use MD5 in Crypto++

#include <cryptlib.h>
#include <md5.h>

#include "Hashing.h"

using namespace CryptoPP;

std::string get_hash(const std::string& str)
{
    std::string digest;
    Weak::MD5 hash;

    hash.Update((const byte*)(str.data()), str.size());
    digest.resize(hash.DIGESTSIZE);
    hash.Final((byte*)&digest[0]);

    return digest;
}
问题
当我在Win10 x64计算机上的VS 2019社区中对此进行编译时,出现此链接错误:
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl CryptoPP::Algorithm::Algorithm(bool)" (??0Algorithm@CryptoPP@@QEAA@_N@Z) referenced in function "public: __cdecl CryptoPP::HashTransformation::HashTransformation(void)" (??0HashTransformation@CryptoPP@@QEAA@XZ)
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2001: unresolved external symbol "public: virtual bool __cdecl CryptoPP::HashTransformation::TruncatedVerify(unsigned char const *,unsigned __int64)" (?TruncatedVerify@HashTransformation@CryptoPP@@UEAA_NPEBE_K@Z)
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2019: unresolved external symbol "public: virtual void __cdecl CryptoPP::IteratedHashBase<unsigned int,class CryptoPP::HashTransformation>::Update(unsigned char const *,unsigned __int64)" (?Update@?$IteratedHashBase@IVHashTransformation@CryptoPP@@@CryptoPP@@UEAAXPEBE_K@Z) referenced in function "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl get_hash(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?get_hash@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBV34@@Z)
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2001: unresolved external symbol "public: virtual unsigned char * __cdecl CryptoPP::IteratedHashBase<unsigned int,class CryptoPP::HashTransformation>::CreateUpdateSpace(unsigned __int64 &)" (?CreateUpdateSpace@?$IteratedHashBase@IVHashTransformation@CryptoPP@@@CryptoPP@@UEAAPEAEAEA_K@Z)
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl CryptoPP::IteratedHashBase<unsigned int,class CryptoPP::HashTransformation>::Restart(void)" (?Restart@?$IteratedHashBase@IVHashTransformation@CryptoPP@@@CryptoPP@@UEAAXXZ)
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl CryptoPP::IteratedHashBase<unsigned int,class CryptoPP::HashTransformation>::TruncatedFinal(unsigned char *,unsigned __int64)" (?TruncatedFinal@?$IteratedHashBase@IVHashTransformation@CryptoPP@@@CryptoPP@@UEAAXPEAE_K@Z)
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2001: unresolved external symbol "protected: virtual unsigned __int64 __cdecl CryptoPP::IteratedHashBase<unsigned int,class CryptoPP::HashTransformation>::HashMultipleBlocks(unsigned int const *,unsigned __int64)" (?HashMultipleBlocks@?$IteratedHashBase@IVHashTransformation@CryptoPP@@@CryptoPP@@MEAA_KPEBI_K@Z)
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2019: unresolved external symbol "public: static void __cdecl CryptoPP::Weak1::MD5::InitState(unsigned int *)" (?InitState@MD5@Weak1@CryptoPP@@SAXPEAI@Z) referenced in function "protected: virtual void __cdecl CryptoPP::IteratedHashWithStaticTransform<unsigned int,struct CryptoPP::EnumToType<enum CryptoPP::ByteOrder,0>,64,16,class CryptoPP::Weak1::MD5,0,0>::Init(void)" (?Init@?$IteratedHashWithStaticTransform@IU?$EnumToType@W4ByteOrder@CryptoPP@@$0A@@CryptoPP@@$0EA@$0BA@VMD5@Weak1@2@$0A@$0A@@CryptoPP@@MEAAXXZ)
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2019: unresolved external symbol "public: static void __cdecl CryptoPP::Weak1::MD5::Transform(unsigned int *,unsigned int const *)" (?Transform@MD5@Weak1@CryptoPP@@SAXPEAIPEBI@Z) referenced in function "protected: virtual void __cdecl CryptoPP::IteratedHashWithStaticTransform<unsigned int,struct CryptoPP::EnumToType<enum CryptoPP::ByteOrder,0>,64,16,class CryptoPP::Weak1::MD5,0,0>::HashEndianCorrectedBlock(unsigned int const *)" (?HashEndianCorrectedBlock@?$IteratedHashWithStaticTransform@IU?$EnumToType@W4ByteOrder@CryptoPP@@$0A@@CryptoPP@@$0EA@$0BA@VMD5@Weak1@2@$0A@$0A@@CryptoPP@@MEAAXPEBI@Z)
C:\Users\myuser\cryptopp\Win32\DLL_Output\Debug\cryptopp.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\myexec.exe : fatal error LNK1120: 9 unresolved externals
  ninja: build stopped: subcommand failed.
我在这里做错了什么?

编辑
在@botje和@nelsonsule的良好反馈之后,我修复了x64问题:
find_library(CRYPTOPP_LIB cryptopp "C:/Users/myuser/cryptopp/x64/DLL_Output/Release")
现在这会导致拱形不匹配警告消失,但是我将收到2个链接错误:
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2019: unresolved external symbol "public: static void __cdecl CryptoPP::Weak1::MD5::InitState(unsigned int *)" (?InitState@MD5@Weak1@CryptoPP@@SAXPEAI@Z) referenced in function "protected: virtual void __cdecl CryptoPP::IteratedHashWithStaticTransform<unsigned int,struct CryptoPP::EnumToType<enum CryptoPP::ByteOrder,0>,64,16,class CryptoPP::Weak1::MD5,0,0>::Init(void)" (?Init@?$IteratedHashWithStaticTransform@IU?$EnumToType@W4ByteOrder@CryptoPP@@$0A@@CryptoPP@@$0EA@$0BA@VMD5@Weak1@2@$0A@$0A@@CryptoPP@@MEAAXXZ)
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\Hashing.cpp.obj : error LNK2019: unresolved external symbol "public: static void __cdecl CryptoPP::Weak1::MD5::Transform(unsigned int *,unsigned int const *)" (?Transform@MD5@Weak1@CryptoPP@@SAXPEAIPEBI@Z) referenced in function "protected: virtual void __cdecl CryptoPP::IteratedHashWithStaticTransform<unsigned int,struct CryptoPP::EnumToType<enum CryptoPP::ByteOrder,0>,64,16,class CryptoPP::Weak1::MD5,0,0>::HashEndianCorrectedBlock(unsigned int const *)" (?HashEndianCorrectedBlock@?$IteratedHashWithStaticTransform@IU?$EnumToType@W4ByteOrder@CryptoPP@@$0A@@CryptoPP@@$0EA@$0BA@VMD5@Weak1@2@$0A@$0A@@CryptoPP@@MEAAXPEBI@Z)
C:\Users\myuser\Documents\myexec\out\build\x64-Debug (default)\myexec.exe : fatal error LNK1120: 2 unresolved externals

最佳答案

C:\Users\myuser\cryptopp\Win32\DLL_Output\Debug\cryptopp.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'您的问题似乎来自这里。您在更高的OS体系结构(x64)上运行,同时链接到为更低的OS体系结构(x86)设计的32位加密库。考虑链接到为x64设计的cyrpto库

关于c++ - 使用CMake构建C++项目时,链接Crypto++失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64334300/

相关文章:

c++ - 为什么在通过模板使用 operator<< 时需要重载?

c++ - 使用 Atmel Studio C++ Build 时无法在 xmega 中使用 ISR

cmake - 如何让cmake自动重新检查依赖项的版本?

linux - arm-linux 下缺少 libopencv_ts 缺失

c++ - ElGamal 加密示例?

c++ - 在 ECIES 中使用之前生成的私钥

c++ - 插入二叉树(不是 BST)不起作用

c++ - 使用C++/Visual Studio从串口读取数据

android studio 3.2初始构建错误(找不到testCCompiler.c文件,windows 10)

c++ - 使用 CryptoPP 库加密和解密字节数组/vector