CMake linking Windows SDK

CMake linking Windows SDK

本文关键字:SDK Windows linking CMake      更新时间:2023-10-16

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

我目前的CMakeList.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:projectPathsoundclass.cpp:4:0:
C:projectPathsoundclass.h:21:24: fatal error: dsound.h: No such file or directory
In file included from C:projectPathmain.cpp:5:0:
C:projectPathsoundclass.h:21:24: fatal error: dsound.h: No such file or directory
compilation terminated.
compilation terminated.
CMakeFilesProject.dirbuild.make:61: recipe for target 'CMakeFiles/Project.dir/main.cpp.obj' failed
CMakeFilesProject.dirbuild.make:85: recipe for target 'CMakeFiles/Project.dir/soundclass.cpp.obj' failed
CMakeFilesMakefile2: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
CMakeFilesMakefile2: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},因为您对包含和库使用相同的变量。

其他提示:

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