C++ 如何在 CMake 项目中使用外部 DLL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17225121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use external DLLs in CMake project
提问by mitjap
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).
我一直在互联网上搜索,但找不到任何可以回答我的问题的内容(或者我不知道要搜索什么)。
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.
无论如何,这是我的问题:我想在我的 CMake 项目中使用 3rdParty 库(.dll 文件)。我想包含的库 ( https://github.com/pitzer/SiftGPU) 是开源的,也有我想使用的二进制文件,如果相关,还可以使用 CMake 作为构建工具。
I hope I was clear enough.
我希望我说得够清楚了。
采纳答案by Guillaume
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:
首先,编辑您的 CMakeLists.txt 以包含您的第三方库。您需要两件事:头文件的路径和要链接的库文件。例如:
# 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})
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 配置你的项目,但由于它没有任何神奇的方法来找到你安装的库,它不会找到任何东西,但它会创建两个缓存变量: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.
使用 CMake GUISIFTGPU_INCLUDE_DIR
指向包含头文件的目录和第三方库SIFTGPU_LIBRARY
的.lib
文件。
Repeat for every third party library, configure again and compile.
对每个第三方库重复,再次配置并编译。