我有build / CMakeLists.txt文件:

cmake_minimum_required (VERSION 2.8)
project(Tetris)

include_directories(
    "headers"
)

set(SFML_DEPENDENCIES)
set(SFML_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} ${SFML_DEPENDENCIES})
set(SFML_DEPENDENCIES ${SFML_GRAPHICS_DEPENDENCIES} ${SFML_DEPENDENCIES})
set(SFML_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} ${SFML_DEPENDENCIES})

set(CMAKE_CXX_COMPILER /usr/bin/g++-5)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g")

set(EXECUTABLE_NAME "tetris")
file(GLOB SOURCES *.cpp)
add_executable(${EXECUTABLE_NAME} ${SOURCES})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})


当我运行cmake .. &&时,屏幕上显示一条消息,提示我不包括SFML库:

undefined reference to `sf::VertexArray::operator[](unsigned long)'
undefined reference to `sf::Keyboard::isKeyPressed(sf::Keyboard::Key)'


等等。我还尝试将标志包含到CMAKE_CXX_FLAGS变量:
-lsfml-graphics -lsfml-window -lsfml-system也不起作用。但是,如果我直接从控制台运行g ++,则所有编译都可以:

g++-5 --std=c++11 -Wall main.cpp Game.cpp Window.cpp Board.cpp -lsfml-graphics -lsfml-window -lsfml-system


如果有任何区别,我会使用Linux Mint。

最佳答案

您需要找到SFML软件包的位置,并添加包含目录和链接。

在您的CMakeLists.txt中:
更改此行:

target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})


通过:

# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics)
if(SFML_FOUND)
  include_directories(${SFML_INCLUDE_DIR})
  target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()


Source

编辑:

对于SFML v1,将find_package行更改为:

find_package(SFML 1 REQUIRED system window graphics)

08-16 11:23