我目前正在学习CMake,而且我正在尝试创建我的第一个测试项目。我可以通过CMake在Visual Studio中启动并运行一个简单的项目。但是,我在尝试添加库时遇到了麻烦。我已经阅读了一些指南和内容,但我一直在收到错误。基本上,我试图在我的示例项目中链接SDL库(游戏编程库)。我已经将这些图书馆放在了最高级别,“第三方”#3;夹。以下是我的顶级目录中的CmakeLists.txt文件:
cmake_minimum_required(VERSION 2.8.11)
project(Hello)
#Find necessary header files
find_path(SDL_INCLUDE_DIR SDL.h HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/include/)
#Find necessary library files
find_library(SDL_LIB_DIR SDL2 HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/lib/x86)
find_library(SDLMAIN_LIB_DIR SDLmain HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/lib/x86)
#Add/Link files to project
include_directories(${SDL_INCLUDE_DIR})
target_link_libraries(Test PUBLIC ${SDL_LIB_DIR})
target_link_libraries(Test PUBLIC ${SDLMAIN_LIB_DIR})
add_executable(Test "${CMAKE_SOURCE_DIR}/Source/Main.cpp")
我不是百分之百确定HINTS参数,但我看到它在另一个线程上使用过。无论如何,这是我一直得到的错误:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
SDLMAIN_LIB_DIR
linked by target "Test" in directory C:/Users/Jason/Desktop/Test
我做错了什么以及如何在CMake中正确链接库?
答案 0 :(得分:2)
find_library
或find_package
,然后cmake将设置一些可用于链接/使用库的变量。我不熟悉SDL,但通过谷歌搜索一下,我会说它应该是这样的:
find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2)
find_library(SDL2_LIBRARY NAME SDL2)
add_executable(MyExec main.cpp)
target_include_directories(MyExec ${SDL2_INCLUDE_DIR})
target_link_libraries(MyExec ${SDL2_LIBRARY})
find_library
将设置变量SDL2_INCLUDE_DIR
和SDL2_LIBRARY
,您可以使用这些变量链接到SDL并将其包含添加到项目中。