JustAnotherStudentDev
JustAnotherStudentDev

Reputation: 41

How to correctly link an external library using CMake?

I'm trying to use the Chilkat C++ library to do a cryptography assignment, but I cannot seem to get the library to link properly. As of right now the header file CkRsa.h cannot be found by the compiler. I've spent a few hours searching across the internet for solutions to no avail. Here is what I have so far (this is all in a Mac OS X environment):

The lib files are installed in /users/Adam/Desktop/chilkat/libDyn and the header files are in /users/Adam/Desktop/chilkat/include Here is my 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)

Here is my 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;
}

Upvotes: 4

Views: 4761

Answers (2)

Florian
Florian

Reputation: 43048

I think this is a simply problem of how you ordered your calls. CMake does parse its scripts sequential. So the include_directories() call has to be put before the add_executable() call:

include_directories("/users/Adam/Desktop/chilkat/include")
add_executable(SocketEncryption ${SOURCE_FILES})

The better way since CMake 2.8.12 would be to let the library propagate its header file paths:

target_include_directories(chilkat PUBLIC "/users/Adam/Desktop/chilkat/include")

Or - for readability - add it directly via target properties:

set_property(
    TARGET chilkat 
    PROPERTY 
        IMPORTED_LOCATION "/users/Adam/Desktop/chilkat/libDyn/libchilkat_x86_64.dylib"
        INTERFACE_INCLUDE_DIRECTORIES "/users/Adam/Desktop/chilkat/include"
)

See also CMake: Creating Relocatable Packages.

References

Upvotes: 1

sailfish009
sailfish009

Reputation: 2927

change your code like this (quotation):

include_directories("/users/Adam/Desktop/chilkat/include")
link_directories("/users/Adam/Desktop/chilkat/libDyn")

Upvotes: 1

Related Questions