使用CMake构建C ++项目时,链接Crypto ++失败 [英] Linking Crypto++ fails when building a C++ project using CMake

查看:109
本文介绍了使用CMake构建C ++项目时,链接Crypto ++失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下项目结构:

myexec/
|-main.cpp
|-hashing.cpp
|-hashing.h
|-CMakeLists.txt

我的小软件需要 Crypto ++ 我在此路径下构建的最新版本:

My little software needs Crypto++ whose latest version I built under this path:

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社区中对此进行编译时,出现此链接错误:

Problem

As I compile this in VS 2019 Community on my Win10 x64 machine, I get this linking error:

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问题:

After good feedback from @botje and @nelsonsule, I fixed the x64 issue:

find_library(CRYPTOPP_LIB cryptopp "C:/Users/myuser/cryptopp/x64/DLL_Output/Release")

这现在导致拱形不匹配警告消失,但是我将收到2个链接错误:

This is now causing the arch mismatch warning to go away, but I will get 2 linking errors:

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:警告LNK4272:库机器类型'x86'与目标机器类型为"x64"

您的问题似乎来自这里.您在更高的OS体系结构( x64 )上运行,同时链接到为更低的OS体系结构( x86 )设计的32位加密库.考虑链接到为 x64

Your problem seems to be coming from here. you're running on a higher architecture of OS (x64) while linking to a crypto library designed for a lower OS architecture (x86) which is a 32 bit. consider linking to a cyrpto library designed for x64

这篇关于使用CMake构建C ++项目时,链接Crypto ++失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆