CMake和Window .Lib文件

时间:2016-02-13 13:55:38

标签: c++ windows cmake

我一直在努力在Stackoverflow上找到这个问题的答案,至少有一个是我理解的。我最近买了CLion,并希望将一个较旧的项目从Visual Studio移植到它。但我不知道如何链接或添加.lib文件到我的项目。我需要链接Xinput和DSound。

在Visual Studio中,我刚添加了标题,然后将这些标题添加到我的文件顶部

#pragma comment(lib,“XInput.lib”)

#pragma comment(lib,“Dsound.lib”)

我完全不知道如何使用CMake链接到这些库,因为我是一个完整的初学者。

欢迎任何帮助

这是我目前的CMake文件

cmake_minimum_required(VERSION 3.3)
project("Handmade_Hero")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)

INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")

add_executable("Handmade_Hero" ${SOURCE_FILES})

更新

cmake_minimum_required(VERSION 3.3)
project(Handmade_Hero)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)

INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")

add_executable(Handmade_Hero ${SOURCE_FILES})

set( LIBS XInput DSound )
target_link_libraries(Handmade_Hero ${LIBS} )

我的编译器现在抛出以下错误:

PATH\ClionProjects\Handmade-Hero\main.cpp:3:20: fatal error: Xinput.h: No such file or directory
 #include <Xinput.h>
                    ^
compilation terminated.
mingw32-make.exe[3]: *** [CMakeFiles/Handmade_Hero.dir/main.cpp.obj] Error 1
CMakeFiles\Handmade_Hero.dir\build.make:62: recipe for target 'CMakeFiles/Handmade_Hero.dir/main.cpp.obj' failed
mingw32-make.exe[2]: *** [CMakeFiles/Handmade_Hero.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Handmade_Hero.dir/rule] Error 2
mingw32-make.exe: *** [Handmade_Hero] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Handmade_Hero.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Handmade_Hero.dir/rule' failed
Makefile:117: recipe for target 'Handmade_Hero' failed

1 个答案:

答案 0 :(得分:1)

只需在CMakeLists.txt

之后添加这些内容即可
SET( LIBS XInput DSound )
TARGET_LINK_LIBRARIES(projectname ${LIBS} )