我正在尝试将SFML作为依赖项包含在我的CMake项目中,它可以很好地编译SFML,但我无法弄清楚如何链接它。
这是我的CMakeLists.txt,附带注释注释:
cmake_minimum_required(VERSION 3.9.1)
set(CMAKE_LEGACY_CYGWIN_WIN32 1)
set (CMAKE_CXX_STANDARD 11) # Set C++11
project(CHIP8)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # static library
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) # dynamic library
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # executables
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/modules" ${CMAKE_MODULE_PATH})
# Include SFML
add_subdirectory(${CMAKE_SOURCE_DIR}/dep/SFML)
include_directories(${CMAKE_SOURCE_DIR}/dep/SFML/include)
#Include where to find headers
include_directories(./src)
include_directories(./src/headers)
# Do a GLOB search, and add them to executable
set(EXECUTABLE_NAME "CHIP8")
file(GLOB SRC_FILES
"src/headers/*.h"
"src/*.cpp"
)
add_executable(${EXECUTABLE_NAME} ${SRC_FILES})
target_link_libraries(${EXECUTABLE_NAME} SFML) #Here is where the problem lies!
正如您所看到的,我正在添加子目录./dep/SFML
,因此它使用自己的CMakeLists进行编译。在下一行包括标题,VSCode选择了。
但是,我在最后一行遇到了麻烦:target_link_libraries
。找不到sfml
SFML
或SFML2
。因此,我不确定如何链接它。
有人有任何想法吗?
感谢。
答案 0 :(得分:1)
SFML不是单个库,而是库,每个库对应一些组件。对于名为
的组件<component-name>
库目标名为
sfml-<component-name>
在代码中使用SFML时,您应该知道您使用的是哪个SFML组件(请参阅SFML文档/教程),并链接到相应的库:
# Assuming you use "graphics" and "system" SFML components
target_link_libraries(<your-executable> sfml-graphics sfml-system)
请注意,此类目标的命名未正式记录,因此将来可以更改。
与SFML链接的首选方式是在配置项目之前(或期间)安装,并使用
find_package(SFML REQUIRED COMPONENTS graphics system)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(<your-executable> ${SFML_LIBRARIES})