我正在尝试使用Chilkat C++库进行加密分配,但是似乎无法正确链接该库。到目前为止,编译器无法找到头文件CkRsa.h。我花了几个小时在互联网上搜索无济于事的解决方案。到目前为止,这是我所拥有的(所有这些都在Mac OS X环境中):
lib文件安装在/users/Adam/Desktop/chilkat/libDyn中,头文件安装在/users/Adam/Desktop/chilkat/include中
这是我的CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(SocketEncryption)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_library(chilkat STATIC IMPORTED)
set_property(
TARGET chilkat
PROPERTY
IMPORTED_LOCATION "/users/Adam/Desktop/chilkat/libDyn/libchilkat_x86_64.dylib"
INTERFACE_INCLUDE_DIRECTORIES "/users/Adam/Desktop/chilkat/include"
)
add_executable(SocketEncryption ${SOURCE_FILES})
target_link_libraries(SocketEncryption chilkat)
这是我的main.cpp
#include <iostream>
#include <CkRsa.h>
//#include <CkPrivateKey.h>
//#include <CkSocket.h>
int main() {
CkRsa alice;
CkRsa bob;
// Key Generation
bool success = alice.GenerateKey(1024);
const char *alicePublicKey = alice.exportPublicKey();
const char *alicePrivateKey = alice.exportPrivateKey();
const char *aliceMessage = "Hi, Bob. How are you?";
// Encryption Step
std::cout << "Encryption started." << std::endl;
CkRsa rsaEncryptor;
rsaEncryptor.put_EncodingMode("hex");
success = rsaEncryptor.ImportPublicKey(alicePublicKey);
bool usePrivateKey = false;
const char * ciphertext = rsaEncryptor.encryptStringENC(aliceMessage, success);
std::cout << ciphertext << std::endl;
return 0;
}
最佳答案
像这样更改您的代码(引号):
include_directories("/users/Adam/Desktop/chilkat/include")
link_directories("/users/Adam/Desktop/chilkat/libDyn")
关于c++ - 如何使用CMake正确链接外部库?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43086011/