Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链接到cmake项目中的动态链接库_C++_Dll_Cmake - Fatal编程技术网

C++ 链接到cmake项目中的动态链接库

C++ 链接到cmake项目中的动态链接库,c++,dll,cmake,C++,Dll,Cmake,在第一步中,我创建了一个test.dll库,其中包含相应的导入库test.dll.a和头文件test.h。我构建了一个新的“客户机”项目,我想在其中使用这个创建的库 我有以下文件结构: client │ └── CMakeLists.txt |── src │ ├── main.cpp │ └── CMakeLists.txt ├── lib │ ├── test.dll │ └── libtest.dll.a │ └── test.h 在rootCmakeLists.txt中,我想添加库 c

在第一步中,我创建了一个
test.dll
库,其中包含相应的导入库
test.dll.a
和头文件
test.h
。我构建了一个新的“客户机”项目,我想在其中使用这个创建的库

我有以下文件结构:

client
│ └── CMakeLists.txt
|── src
│ ├── main.cpp
│ └── CMakeLists.txt
├── lib
│ ├── test.dll
│ └── libtest.dll.a
│ └── test.h
在root
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:没有这样的文件或目录
,并且
无法打开源代码文件test.h


有人知道如何将DLL正确添加到cmake项目中吗?

需要将属性
接口\u INCLUDE\u目录添加到测试目标中

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_子目录(lib)
命令,因为您没有将CMakeLists.txt文件放在那里,并且不需要在此目录中构建任何内容