简体   繁体   English

如何使用CMake正确链接外部库?

[英]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. 我正在尝试使用Chilkat C ++库进行加密分配,但似乎无法正确链接该库。 As of right now the header file CkRsa.h cannot be found by the compiler. 到目前为止,编译器无法找到头文件CkRsa.h。 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): 这是到目前为止我所拥有的(这一切都在Mac OS X环境中):

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 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)

Here is my main.cpp 这是我的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;
}

change your code like this (quotation): 像这样更改您的代码(引号):

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

I think this is a simply problem of how you ordered your calls. 我认为这只是您如何订购电话的简单问题。 CMake does parse its scripts sequential. CMake会顺序解析其脚本。 So the include_directories() call has to be put before the add_executable() call: 因此必须将include_directories()调用放在add_executable()调用之前:

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: 自CMake 2.8.12起,更好的方法是让库传播其头文件路径:

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 . 另请参阅CMake:创建可重定位软件包

References 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM