我有 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 ..&& make 我有一个消息屏幕,说我没有包含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。
答案 0 :(得分:1)
您需要找到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()
<强> EDITED 强>:
对于SFML v1,请更改find_package
行:
find_package(SFML 1 REQUIRED system window graphics)