简体   繁体   English

从源代码编译在CMake中链接SFML

[英]Linking SFML within CMake, compiling from sources

I'm trying to include SFML as a dependency in my CMake project, and it compiles SFML fine, but I can't figure out how to link it. 我试图将SFML作为依赖项包含在我的CMake项目中,并且它可以很好地编译SFML,但是我不知道如何链接它。

Here's my CMakeLists.txt, with annotated comments: 这是我的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!

As you can see, I'm adding the subdirectory ./dep/SFML so it compiles with its own CMakeLists. 如您所见,我正在添加子目录./dep/SFML以便它使用自己的CMakeLists进行编译。 Including the headers too on the next line, which VSCode picks up. 下一行也包括标题,VSCode会选择这些标题。

However, I'm having trouble on the last line: target_link_libraries . 但是,我在最后一行遇到麻烦: target_link_libraries It can't find sfml SFML or SFML2 . 它找不到sfml SFMLSFML2 Therefore, I'm not sure how to link it. 因此,我不确定如何链接它。

Anyone have any idea? 有人知道吗

Thanks. 谢谢。

SFML is not a single library, but a set of libraries , each of which corresponds to some component . SFML不是单个库,而是一组库 ,每个都对应于某个组件 For component named 对于名为

<component-name>

the library target is named 库目标名为

sfml-<component-name>

When use SFML in your code, you should know which SFML component(s) you use (see SFML docs/tutorials), and link with corresponded libraries: 在代码中使用SFML时,您应该知道所使用的SFML组件(请参阅SFML文档/教程),并链接到相应的库:

# Assuming you use "graphics" and "system" SFML components
target_link_libraries(<your-executable> sfml-graphics sfml-system)

Note, that such targets' naming is not officially documented, so it can be changed in the future. 请注意,此类目标的命名未正式记录,因此将来可以更改。

Preferred way for link with SFML is to install it before (or during) configuring your project, and use 与SFML链接的首选方法是在配置项目之前(或期间)进行安装并使用

find_package(SFML REQUIRED COMPONENTS graphics system)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(<your-executable> ${SFML_LIBRARIES})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM