我的项目有:
conanfile.txt
[tool_requires]
cmake/3.27.1
[requires]
nlohmann_json/3.11.2
libcurl/8.2.1
openssl/3.1.2
[generators]
CMakeDeps
CMakeToolchain
CMakeLists.txt
cmake_minimum_required(VERSION 3.27.1)
project(Testing
VERSION 1.0
DESCRIPTION "Testing Application"
LANGUAGES CXX)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_SWIG_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -v")
find_package(CURL REQUIRED)
message(${CURL_INCLUDE_DIRS})
message(${CURL_LIBRARIES})
find_package(OpenSSL REQUIRED)
find_package(nlohmann_json REQUIRED)
include_directories(${CURL_INCLUDE_DIRS})
include_directories(${OpenSSL_INCLUDE_DIR})
include_directories(${nlohmann_json_INCLUDE_DIR})
add_executable(e_test encrypt_test.cpp )
target_link_libraries(e_test CURL::libcurl ${OpenSSL_LIBRARIES} nlohmann_json::nlohmann_json )
CPP 文件:
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/err.h>
int main() {
// Initialize the OpenSSL library
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
// Load the recipient's public key
EVP_PKEY* publicKey = nullptr;
FILE* publicKeyFile = fopen("../public_key.pem", "rb"); // Load the recipient's public key
if (publicKeyFile) {
publicKey = PEM_read_PUBKEY(publicKeyFile, nullptr, nullptr, nullptr);
fclose(publicKeyFile);
}
else {
std::cerr << "Error loading public key." << std::endl;
return 1;
}
// Message to encrypt
std::string message = "Hello, OpenSSL!";
// Buffer for encrypted message
unsigned char* encryptedMessage = nullptr;
size_t encryptedMessageLength = 0;
// Create an EVP context
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(publicKey, nullptr);
// Initialize the encryption operation
if (EVP_PKEY_encrypt_init(ctx) == 1 &&
EVP_PKEY_encrypt(ctx, nullptr, &encryptedMessageLength, reinterpret_cast<const unsigned char*>(message.c_str()), message.length()) == 1) {
std::cout<<"I am here\n";
encryptedMessage = new unsigned char[encryptedMessageLength];
if (EVP_PKEY_encrypt(ctx, encryptedMessage, &encryptedMessageLength, reinterpret_cast<const unsigned char*>(message.c_str()), message.length()) != 1) {
std::cerr << "Encryption error." << std::endl;
return 1;
}
}
// Print or process the encrypted message as needed
std::cout << "Encrypted message length: " << encryptedMessageLength <<"\n" ;
std::cout << "Encrypted message: " << std::string(reinterpret_cast<const char*>(encryptedMessage), encryptedMessageLength) << std::endl;
// Clean up
delete[] encryptedMessage;
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(publicKey);
ERR_free_strings();
return 0;
}
错误:
Undefined symbols for architecture x86_64:
"_EVP_PKEY_CTX_free", referenced from:
_main in encrypt_test.cpp.o
"_EVP_PKEY_CTX_new", referenced from:
_main in encrypt_test.cpp.o
"_EVP_PKEY_encrypt", referenced from:
_main in encrypt_test.cpp.o
"_EVP_PKEY_encrypt_init", referenced from:
_main in encrypt_test.cpp.o
"_EVP_PKEY_free", referenced from:
_main in encrypt_test.cpp.o
"_OPENSSL_init_crypto", referenced from:
_main in encrypt_test.cpp.o
"_PEM_read_PUBKEY", referenced from:
_main in encrypt_test.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
我通过以下方式调用 CMAKE:
#!/bin/bash
conan profile detect
conan install . --output-folder=build --build=missing
cd build
sh conanrun.sh
cmake -G "Ninja" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Debug .. && cmake --build . --config Debug
sh deactivate_conanrun.sh
注意:删除 conanrun.sh 并停用脚本不会产生 区别。
使用命令行构建时,此代码可以完美运行;当柯南没有出现时,这也运行良好。我已经调试过cmake是否能够从conan build中找到必要的库文件和头文件;它正在这样做。 我不确定我在这里做错了什么
由于这个原因,调试构建不起作用;按照文档使用
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release && cmake --build .
解决了问题