如何在CMake项目中使用外部DLL [英] How to use external DLLs in CMake project

查看:216
本文介绍了如何在CMake项目中使用外部DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索互联网,但找不到任何可以回答我问题的内容(或者我不知道要搜索什么)。

I've been searching over the internet but I couldn't find anything that would answer my question (or I don't know what to search for).

无论如何这里是我的问题:
我想在我的CMake项目中使用3rdParty库(.dll文件)。我想包括的图书馆( https://github.com/pitzer/SiftGPU )是开源的,也可用二进制,我想使用,并使用CMake作为构建工具,如果这是相关的。

Anyway here's my issue: I want to use 3rdParty libraries (.dll files) in my CMake project. Library (https://github.com/pitzer/SiftGPU) that I want to include is open source and is also available in binary which I would like to use and also uses CMake as build tool if that's relevant.

我希望我足够清楚。

推荐答案

您的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 的目录您的第三方库的 .lib 文件。

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.

这篇关于如何在CMake项目中使用外部DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆