且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在CMake项目中使用外部DLL

更新时间:2022-06-13 01:19:43

您的CMakeLists.txt以包括您的第三方库。你需要两个东西:路径到头文件和库文件链接。例如:

First, edit your CMakeLists.txt to include your third party library. You'll need two thing: path to header files and library file to link to. For instance:

# searching for include directory
find_path(SIFTGPU_INCLUDE_DIR siftgpu.h)

# searching for library file
find_library(SIFTGPU_LIBRARY siftgpu)

if (SIFTGPU_INCLUDE_DIR AND SIFTGPU_LIBRARY)
    # you may need that if further action in your CMakeLists.txt depends
    # on detecting your library
    set(SIFTGPU_FOUND TRUE)

    # you may need that if you want to conditionally compile some parts
    # of your code depending on library availability
    add_definitions(-DHAVE_LIBSIFTGPU=1)

    # those two, you really need
    include_directories(${SIFTGPU_INCLUDE_DIR})
    set(YOUR_LIBRARIES ${YOUR_LIBRARIES} ${SIFTGPU_LIBRARY})
endif ()



接下来,您可以对其他库执行相同操作,检测到库,链接到目标:

Next, you can do the same for other libraries and when every libraries are detected, link to the target:

target_link_libraries(yourtarget ${YOUR_LIBRARIES})

然后你可以配置你的项目与CMake,但因为它没有任何魔法的方式来找到你安装的库,它不会找到任何东西,但它会创建两个缓存变量: SIFTGPU_INCLUDE_DIR SIFTGPU_LIBRARY

Then you can configure your project with CMake, but as it doesn't have any magic way to find your installed library, it won't find anything, but it'll create two cache variables: SIFTGPU_INCLUDE_DIR and SIFTGPU_LIBRARY.

使用CMake GUI使 SIFTGPU_INCLUDE_DIR 指向包含头文件和 SIFTGPU_LIBRARY 的目录

Use the CMake GUI to have SIFTGPU_INCLUDE_DIR pointing to the directory containing the header files and SIFTGPU_LIBRARY to the .lib file of your third party library.

对每个第三方库重复上述步骤,再次配置并编译。

Repeat for every third party library, configure again and compile.