Cannot compile SFML project using cmake

那年仲夏 提交于 2021-01-28 06:06:52

问题


I am trying to set up a new c++/sfml project using cmake/g++ on windows but the build keeps on failing with linking errors. There are still errors if I use SFML_STATIC or not. I have looked up the problem but none of the answers so far remove the errors.

- \build
- \cmake_modules
  - FindSFML.cmake
- \SFML
- CMakeLists.txt
- config.h.in
- main.cpp

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
project(myproject)

set(myproject_VERSION_MAJOR 1)
set(myproject_VERSION_MINOR 0)
configure_file(
    "${PROJECT_SOURCE_DIR}/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h"
)
include_directories("${PROJECT_BINARY_DIR}")

set(EXECUTABLE_NAME "myproject")


set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML")
find_package(SFML 2 REQUIRED network audio graphics window system)
include_directories(${SFML_INCLUDE_DIR})
add_executable(${EXECUTABLE_NAME} main.cpp)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})

install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin)

include(InstallRequiredSystemLibraries)
set(CPACK_PACKAGE_VERSION_MAJOR "${myproject_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${myproject_VERSION_MINOR}")
include(CPack)

Output -

-- Found SFML 2.4.2 in C:/Users/[project dir]/SFML/include
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/[project dir]/build
Scanning dependencies of target myproject
[ 50%] Building CXX object CMakeFiles/myproject.dir/main.cpp.obj
[100%] Linking CXX executable myproject.exe
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xf5): undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x11b): undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x157): undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x18d): undefined reference to `_imp___ZN2sf11CircleShapeC1Efj'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x19e): undefined reference to `_imp___ZN2sf5Color5GreenE'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1a8): undefined reference to `_imp___ZN2sf5Shape12setFillColorERKNS_5ColorE'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1c8): undefined reference to `_imp___ZN2sf6Window5closeEv'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1dd): undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x20f): undefined reference to `_imp___ZN2sf5ColorC1Ehhhh'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x22a): undefined reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x23d): undefined reference to `_imp___ZN2sf12RenderStates7DefaultE'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x251): undefined reference to `_imp___ZN2sf12RenderTarget4drawERKNS_8DrawableERKNS_12RenderStatesE'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x263): undefined reference to `_imp___ZN2sf6Window7displayEv'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x272): undefined reference to `_imp___ZNK2sf6Window6isOpenEv'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x29b): undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x2c2): undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x2fc): undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0xe): undefined reference to `_imp___ZTVN2sf11CircleShapeE'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0x1c): undefined reference to `_imp___ZTVN2sf11CircleShapeE'
CMakeFiles\myproject.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0x2c): undefined reference to `_imp___ZN2sf5ShapeD2Ev'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\myproject.dir\build.make:101: recipe for target 'myproject.exe' failed
mingw32-make[2]: *** [myproject.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/myproject.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/myproject.dir/all] Error 2
Makefile:150: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

来源:https://stackoverflow.com/questions/45936971/cannot-compile-sfml-project-using-cmake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!