Reputation: 457
I have been struggling to find an answer to this question across Stackoverflow, at least one that I understand. I recently bought CLion and want to port an older project from Visual Studio over to it. But I have no idea how to link or add .lib files to my project. I need to link Xinput and DSound.
In Visual Studio I just added the headers and then added these to the top of my file
#pragma comment(lib, "XInput.lib")
#pragma comment(lib, "Dsound.lib")
I just have absolutely no idea how to link to those libs using CMake as I am a complete beginner with it.
Any Help would be gladly appreciated
This is my current CMake file
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})
UPDATE
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} )
My Compiler now throws the following errors:
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
Upvotes: 5
Views: 10979
Reputation: 1645
Simply add these after your CMakeLists.txt
SET( LIBS XInput DSound )
TARGET_LINK_LIBRARIES(projectname ${LIBS} )
Upvotes: 1