Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将SFML与CMake FetchContent一起使用?_Cmake_Sfml - Fatal编程技术网

如何将SFML与CMake FetchContent一起使用?

如何将SFML与CMake FetchContent一起使用?,cmake,sfml,Cmake,Sfml,所以我想直接使用cmakefetchcontent从git标签获取SFML。大部分教程都没有用到这个,所以我真的不知道该怎么做,我用它作为参考 我的CMakeLists.txt cmake_minimum_required(VERSION 3.10) include(FetchContent) project(2DComputerGraphics) set(CMAKE_CXX_STANDARD 17) # SFML set(SFML_VERSION "2.5.1") FetchConten

所以我想直接使用cmakefetchcontent从git标签获取SFML。大部分教程都没有用到这个,所以我真的不知道该怎么做,我用它作为参考

我的CMakeLists.txt

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)
它给了我这个错误

/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。如何解决这个问题?

变量是错误的。必须小写的状态。squareskittles也是正确的,您不应该使用${SFML_LIBRARIES}/${SFML_INCLUDE_DIR},这仅在使用旧的FindSFML.cmake时有效


SFML变量(如SFML_库)是否正确填充?SFML文件中的文档表明不再填充这些变量,而是填充目标,如SFML窗口和SFML-graphics。该死,我才意识到我一直在使用‑8209字符,而不是常规的-45!我直接从
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)