首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在CMake FetchContent中使用SFML?

如何在CMake FetchContent中使用SFML?
EN

Stack Overflow用户
提问于 2020-01-03 05:24:21
回答 1查看 992关注 0票数 0

因此,我想直接使用CMake FetchContent从git标记获取SFML。本教程大部分都没有使用这个,所以我真的不知道该做什么,我使用imgui-sfml-获取内容作为参考。

我的CMakeLists.txt

代码语言:javascript
代码运行次数:0
运行
复制
cmake_minimum_required(VERSION 3.10)
include(FetchContent)

project(2DComputerGraphics)

set(CMAKE_CXX_STANDARD 17)

# SFML
set(SFML_VERSION "2.5.1")
FetchContent_Declare(
    SFML
    GIT_REPOSITORY "https://github.com/SFML/SFML.git"
    GIT_TAG        "${SFML_VERSION}"
)
FetchContent_GetProperties(SFML)
if(NOT SFML_POPULATED)
    FetchContent_Populate(SFML)
    add_subdirectory(${SFML_SOURCE_DIR} ${SFML_BINARY_DIR})

    #
    # message("SFML_SOURCE_DIR: ${SFML_SOURCE_DIR}")
    # message("SFML_BINARY_DIR: ${SFML_BINARY_DIR}")
endif()

set(SOURCE
    "main.cpp"
)

#
# message("Source: ${SOURCE}")

add_executable(2DComputerGraphicsApp
    "${SOURCE}"
)

target_include_directories(2DComputerGraphicsApp PRIVATE
    "${SFML_INCLUDE_DIR}"
)

#
# message("SFML_INCLUDE_DIR: ${SFML_INCLUDE_DIR}")

target_link_libraries(2DComputerGraphicsApp
    "${SFML_LIBRARIES}"
    "${SFML_DEPENDENCIES}"
)

#
# message("SFML_LIBRARIES: ${SFML_LIBRARIES}")
# message("SFML_DEPENDENCIES: ${SFML_DEPENDENCIES}")

target_compile_options(2DComputerGraphicsApp PRIVATE -Wall)

它给了我这个错误

代码语言:javascript
代码运行次数:0
运行
复制
/usr/bin/ld: CMakeFiles/2DComputerGraphicsApp.dir/main.cpp.o: in function `main':
main.cpp:(.text+0x82): undefined reference to `sf::String::String(char const*, std::locale const&)'
/usr/bin/ld: main.cpp:(.text+0xa0): undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
/usr/bin/ld: main.cpp:(.text+0xd3): undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
/usr/bin/ld: main.cpp:(.text+0x100): undefined reference to `sf::Texture::Texture()'
/usr/bin/ld: main.cpp:(.text+0x119): undefined reference to `sf::Texture::create(unsigned int, unsigned int)'
/usr/bin/ld: main.cpp:(.text+0x132): undefined reference to `sf::Sprite::Sprite(sf::Texture const&)'
/usr/bin/ld: main.cpp:(.text+0x152): undefined reference to `sf::Window::isOpen() const'
/usr/bin/ld: main.cpp:(.text+0x173): undefined reference to `sf::Window::pollEvent(sf::Event&)'
/usr/bin/ld: main.cpp:(.text+0x190): undefined reference to `sf::Window::close()'
/usr/bin/ld: main.cpp:(.text+0x1da): undefined reference to `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)'
/usr/bin/ld: main.cpp:(.text+0x1f7): undefined reference to `sf::RenderTarget::clear(sf::Color const&)'
/usr/bin/ld: main.cpp:(.text+0x210): undefined reference to `sf::Texture::update(unsigned char const*)'
/usr/bin/ld: main.cpp:(.text+0x229): undefined reference to `sf::RenderStates::Default'
/usr/bin/ld: main.cpp:(.text+0x234): undefined reference to `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)'
/usr/bin/ld: main.cpp:(.text+0x243): undefined reference to `sf::Window::display()'
/usr/bin/ld: main.cpp:(.text+0x284): undefined reference to `sf::Texture::~Texture()'
/usr/bin/ld: main.cpp:(.text+0x293): undefined reference to `sf::RenderWindow::~RenderWindow()'
/usr/bin/ld: main.cpp:(.text+0x2fd): undefined reference to `sf::Texture::~Texture()'
/usr/bin/ld: main.cpp:(.text+0x311): undefined reference to `sf::RenderWindow::~RenderWindow()'
/usr/bin/ld: CMakeFiles/2DComputerGraphicsApp.dir/main.cpp.o: in function `sf::Sprite::~Sprite()':
main.cpp:(.text._ZN2sf6SpriteD2Ev[_ZN2sf6SpriteD5Ev]+0xf): undefined reference to `vtable for sf::Sprite'
/usr/bin/ld: main.cpp:(.text._ZN2sf6SpriteD2Ev[_ZN2sf6SpriteD5Ev]+0x1d): undefined reference to `vtable for sf::Sprite'
/usr/bin/ld: main.cpp:(.text._ZN2sf6SpriteD2Ev[_ZN2sf6SpriteD5Ev]+0x35): undefined reference to `sf::Transformable::~Transformable()'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/2DComputerGraphicsApp.dir/build.make:84: 2DComputerGraphicsApp] Error 1
make[3]: Leaving directory '/home/andraantariksa/Projects/2d-computer-graphics/build'
make[2]: *** [CMakeFiles/Makefile2:216: CMakeFiles/2DComputerGraphicsApp.dir/all] Error 2
make[2]: Leaving directory '/home/andraantariksa/Projects/2d-computer-graphics/build'
make[1]: *** [Makefile:130: all] Error 2
make[1]: Leaving directory '/home/andraantariksa/Projects/2d-computer-graphics/build'
make: *** [Makefile:2: build] Error 2

我认为发生这种情况是因为我没有正确地链接SFML。我该怎么解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-03 19:36:09

变量是错的。https://cmake.org/cmake/help/v3.16/module/FetchContent.html指出,<name>必须是小写。同样地,squareskittles是正确的,您不应该使用${SFML_LIBRARIES} / ${SFML_INCLUDE_DIR},这只有在使用旧的FindSFML.cmake时才有效。

代码语言:javascript
代码运行次数:0
运行
复制
cmake_minimum_required(VERSION 3.10)

include(FetchContent)

project(2DComputerGraphics)

set(CMAKE_CXX_STANDARD 17)

set(SFML_VERSION "2.5.1")

FetchContent_Declare(
    sfml
    GIT_REPOSITORY "https://github.com/SFML/SFML.git"
    GIT_TAG        "${SFML_VERSION}"
)

FetchContent_GetProperties(sfml)
if(NOT sfml_POPULATED)
    FetchContent_Populate(sfml)
    add_subdirectory(${sfml_SOURCE_DIR} ${sfml_BINARY_DIR})
endif()

set(SOURCE
    main.cpp
)

add_executable(2DComputerGraphicsApp
    ${SOURCE}
)

target_link_libraries(2DComputerGraphicsApp
    PRIVATE
        sfml-audio
        sfml-graphics
        sfml-system
        sfml-window
)

target_compile_options(2DComputerGraphicsApp PRIVATE -Wall)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59573574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档