Link 到 cmake 项目中的动态 link 库
Link to a dynamic link library in a cmake project
第一步,我创建了一个 test.dll
库,其中包含相应的导入库 test.dll.a
和头文件 test.h
。我构建了一个新的“客户端”项目,我想在其中使用这个创建的库。
我有以下文件结构:
client
│ └── CMakeLists.txt
|── src
│ ├── main.cpp
│ └── CMakeLists.txt
├── lib
│ ├── test.dll
│ └── libtest.dll.a
│ └── test.h
在根目录中CmakeLists.txt
我想添加库
cmake_minimum_required(VERSION 3.6)
project(client)
add_subdirectory(lib)
add_library(test SHARED IMPORTED GLOBAL)
set_target_properties(
test
PROPERTIES
IMPORTED_LOCATION /path/client/lib/test.dll
IMPORTED_IMPLIB /path/client/lib/libtest.dll.a
)
add_subdirectory(src)
并且在 src CmakeLists.txt
文件中我写了:
add_executable(main main.cpp)
target_link_libraries(main PRIVATE test)
在客户端 main.cpp
我通过 #include "test.h"
添加了库。
我收到错误消息:test.h: No such file or directory
和 cannot open source file test.h
。
有人知道如何将DLL正确添加到cmake项目中吗?
需要将属性INTERFACE_INCLUDE_DIRECTORIES
添加到测试目标中
set_target_properties(
test
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES /path/client/lib
IMPORTED_LOCATION /path/client/lib/test.dll
IMPORTED_IMPLIB /path/client/lib/libtest.dll.a
)
注意:可以安全删除 add_subdirectory(lib)
命令,因为您没有将 CMakeLists.txt 文件放在那里,并且不需要在此目录中构建任何内容。
第一步,我创建了一个 test.dll
库,其中包含相应的导入库 test.dll.a
和头文件 test.h
。我构建了一个新的“客户端”项目,我想在其中使用这个创建的库。
我有以下文件结构:
client
│ └── CMakeLists.txt
|── src
│ ├── main.cpp
│ └── CMakeLists.txt
├── lib
│ ├── test.dll
│ └── libtest.dll.a
│ └── test.h
在根目录中CmakeLists.txt
我想添加库
cmake_minimum_required(VERSION 3.6)
project(client)
add_subdirectory(lib)
add_library(test SHARED IMPORTED GLOBAL)
set_target_properties(
test
PROPERTIES
IMPORTED_LOCATION /path/client/lib/test.dll
IMPORTED_IMPLIB /path/client/lib/libtest.dll.a
)
add_subdirectory(src)
并且在 src CmakeLists.txt
文件中我写了:
add_executable(main main.cpp)
target_link_libraries(main PRIVATE test)
在客户端 main.cpp
我通过 #include "test.h"
添加了库。
我收到错误消息:test.h: No such file or directory
和 cannot open source file test.h
。
有人知道如何将DLL正确添加到cmake项目中吗?
需要将属性INTERFACE_INCLUDE_DIRECTORIES
添加到测试目标中
set_target_properties(
test
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES /path/client/lib
IMPORTED_LOCATION /path/client/lib/test.dll
IMPORTED_IMPLIB /path/client/lib/libtest.dll.a
)
注意:可以安全删除 add_subdirectory(lib)
命令,因为您没有将 CMakeLists.txt 文件放在那里,并且不需要在此目录中构建任何内容。