1

My project has:

  1. 1 cpp file
  2. Conan2.0 as package manager
  3. CMake The files are as follows:

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

8
  • 1
    Why ${OpenSSL_LIBRARIES} (which should be ${OPENSSL_LIBRARIES}) rather than linking to a target (OpenSSL::SSL OpenSSL::Crypto) as you have done with the other libraries? Note that your include_directories calls are unnecessary when lining to targets and they avoid issues with variable name typos Commented Sep 8, 2023 at 10:18
  • Hey @AlanBirtles we need the include_directories otherwise the headerfiles aren't found ( I tried that ). Also, even after linking OpenSSL::SSL OpenSSL::Crypto instead of ${OPENSSL_LIBRARIES} gave the same error. Also, OpenSSL_Libraries link to the same OPENSSL Libraries; I have checked it as well Commented Sep 8, 2023 at 10:44
  • sounds like there's something very wrong with your project if you have to manually specify include directories. How are you invoking cmake? Commented Sep 8, 2023 at 11:28
  • @AlanBirtles My requirement is, i have to run this code in a different device where the packages might not be preinstalled. So i am running conan to install the packages first and create this executable. So, i need to specify these libraries and headers explicitly. That's how the conan documentation asked me to do. But somehow i am doing it wrong!! Commented Sep 8, 2023 at 11:32
  • There's no include_directories in the documentation? How are you invoking cmake? Commented Sep 8, 2023 at 11:34

1 Answer 1

2

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 .
Sign up to request clarification or add additional context in comments.

1 Comment

ah, yes you need to pass -s build_type=Debug to conan or create a debug profile docs.conan.io/2/tutorial/consuming_packages/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.