c++ - CMake 链接 Windows SDK

标签 c++ windows cmake include static-libraries

我在 CLion 中有一个项目,我需要在其中包含一个来自 Windows 8.1 SDK 的库,因此我尝试使用 CMake 来链接它。
我没有任何 .so 或 .dll,只有 .lib(或直接是 .h 文件)。
具体来说,这个库就是dsound.h。
我尝试查找相关问题,但找到的解决方案均不适合我:我该怎么做?

我当前的CMakeLists.txt是

cmake_minimum_required(VERSION 3.7)

project(Project)

set(CMAKE_CXX_STANDARD 11)

include_directories(${WinSDK})

set(SOURCE_FILES main.cpp file1.h file1.cpp)
add_executable(Project ${SOURCE_FILES})
target_link_libraries(Project ${WinSDK})

其中 WinSDK 是包含 Windows SDK 位置的变量。

我得到的错误如下:

[ 33%] Building CXX object CMakeFiles/Project.dir/main.cpp.obj
[ 66%] Building CXX object CMakeFiles/Project.dir/soundclass.cpp.obj
In file included from C:\projectPath\soundclass.cpp:4:0:
C:\projectPath\soundclass.h:21:24: fatal error: dsound.h: No such file or directory
In file included from C:\projectPath\main.cpp:5:0:
C:\projectPath\soundclass.h:21:24: fatal error: dsound.h: No such file or directory
compilation terminated.
compilation terminated.
CMakeFiles\Project.dir\build.make:61: recipe for target 'CMakeFiles/Project.dir/main.cpp.obj' failed
CMakeFiles\Project.dir\build.make:85: recipe for target 'CMakeFiles/Project.dir/soundclass.cpp.obj' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Project.dir/all' failed
mingw32-make.exe[3]: *** [CMakeFiles/Project.dir/main.cpp.obj] Error 1
mingw32-make.exe[3]: *** Waiting for unfinished jobs....
mingw32-make.exe[3]: *** [CMakeFiles/Project.dir/soundclass.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Project.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Project.dir/rule' failed
Makefile:117: recipe for target 'Project' failed
mingw32-make.exe[1]: *** [CMakeFiles/Project.dir/rule] Error 2
mingw32-make.exe: *** [Project] Error 2

最佳答案

${WinSDK} 无法正确设置,因为您对 include 和 library 使用相同的变量。

其他提示:

  • CMAKE_CXX_STANDARD 对 MSVC 编译器没有任何作用
  • 最好使用 target_include_directories() 比使用 include_directories() 来保持目标信息更清晰

关于c++ - CMake 链接 Windows SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44998604/

相关文章:

c++ - 乘以 float 并保持/获得 double 精度

python - Travis CI 在 macOS 上找不到 python3

c++ - 如何在 C++ 中测试相关对象的相等性?

c++ - 错误 : passing ‘const Cell’ as ‘this’ argument of ‘void Cell::printcell()’ discards qualifiers [-fpermissive]

c++ - c++11 严格别名规则是否允许通过 char *、char(&)[N]、甚至 std::array<char, N>& 使用 -fstrict-aliasing -Wstrict-aliasing=2 访问 uint64_t?

Python Git Bash CMD 脚本

c++ - 如何在 Windows 关闭时执行功能

C++ 无法在注册表中创建新键

cmake安装: how to remove subdir prefix

c++ - 将 CMAKE 变量添加到 KDevelop 中的项目