如何在cmake中为链接器设置库?

时间:2015-05-09 18:18:26

标签: c++ linker cmake

我已经下载了SDL库并能够设置包含路径而没有任何麻烦,但设置链接器是一个完整的噩梦......我想如何指出cmake纠正文件?

cmake_minimum_required(VERSION 3.2)
project(simple_timeline_recorder)

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

set(RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build)
set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/build/")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/build/")

include_directories("${PROJECT_SOURCE_DIR}/Dependencies/SDL/include")
target_link_libraries(simple_timeline_recorder)

set(SOURCE_FILES src/main.cpp)
add_executable(simple_timeline_recorder ${SOURCE_FILES})

〜感谢您的帮助

编辑(vsoftco)>

不幸的是,由于某种原因它不起作用。我使用了来自jetbrains的CLion和mingw。我尝试过延伸而没有尝试,我也试过给出绝对路径。没用错误/异常是一样的。

...

include_directories("${PROJECT_SOURCE_DIR}/Dependencies/SDL/include")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -LDependencies/SDL/lib/win32/SDL2")

set(SOURCE_FILES src/main.cpp)
add_executable(simple_timeline_recorder ${SOURCE_FILES})

target_link_libraries(simple_timeline_recorder SDL2)

错误讯息:

Linking CXX executable simple_timeline_recorder.exe
d:/apps/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lSDL2
collect2.exe: error: ld returned 1 exit status
CMakeFiles\simple_timeline_recorder.dir\build.make:87: recipe for target 'simple_timeline_recorder.exe' failed
CMakeFiles\Makefile2:59: recipe for target 'CMakeFiles/simple_timeline_recorder.dir/all' failed
mingw32-make.exe[2]: *** [simple_timeline_recorder.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/simple_timeline_recorder.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
Makefile:74: recipe for target 'all' failed

1 个答案:

答案 0 :(得分:2)

您似乎没有指定要链接的库的路径。 假设您要包含mylib.so中的/path/to/mylib。您可以做的是添加到CMakeLists.txt

SET (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/path/to/mylib")

然后在CMakeLists.txt的最后一行(在ADD_EXECUTABLE子句之后)使用

TARGET_LINK_LIBRARIES(simple_timeline_recorder mylib)

更新编辑

尝试

SET (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L${PROJECT_SOURCE_DIR}/Dependencies/SDL/lib/win32/SDL2")

所以你可以确定你找到了正确的道路。