My project has:
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 File:
#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;
}
The error:
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.
I am invoking the CMAKE in the following manner:
#!/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
Note: Removing conanrun.sh and deactivating script doesn't make a difference.
When built using command line, this code runs flawlessly; this was also running fine when conan wasn't introduced. I have debugged if cmake is able to find the necessary library files and header files from conan build; and it is doing so. I am not sure what i am doing wrong here
Debug Build didn't work for this cause; Following the documentation using the following solved the issue:
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release && cmake --build .