I'm trying to build a CMake project on Windows/MinGW and link it to SFML 2.5.1. CMake seems to find the libraries, and the program compiles fine, but I'm getting 'undefined reference' linker errors. I followed the SFML 2.5 CMake build instructions. What am I missing?
Errors:
[ 50%] Linking CXX executable pressure.exe
CMakeFiles\pressure.dir/objects.a(main.cpp.obj): In function `main':
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf6StringC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6locale'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
C:/code/cpp/small/pressure/main.cpp:21: undefined reference to `_imp___ZNK2sf6Window6isOpenEv'
C:/code/cpp/small/pressure/main.cpp:24: undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
C:/code/cpp/small/pressure/main.cpp:26: undefined reference to `_imp___ZN2sf6Window5closeEv'
C:/code/cpp/small/pressure/main.cpp:30: undefined reference to `_imp___ZN2sf5ColorC1Ehhhh'
C:/code/cpp/small/pressure/main.cpp:30: undefined reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
C:/code/cpp/small/pressure/main.cpp:32: undefined reference to `_imp___ZN2sf6Window7displayEv'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
collect2.exe: error: ld returned 1 exit status
My project's CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(pressure)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)
if(WIN32)
#set(SFML_STATIC_LIBRARIES TRUE)
set(SFML_DIR C:/lib/cpp/SFML-2.5.1/lib/cmake/SFML)
endif()
set(SFML_LIBRARIES sfml-graphics sfml-audio)
set(pressure_VERSION_MAJOR 0)
set(pressure_VERSION_MINOR 1)
configure_file(
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
include_directories("${PROJECT_BINARY_DIR}")
set(EXECUTABLE_NAME "pressure")
find_package(SFML 2.5 COMPONENTS system window graphics audio main network REQUIRED)
add_executable(pressure main.cpp)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
This error has been mentioned a few times on the Stack Overflow site:
More often than not, this specific linker error appears to occur when there are incompatibilities between the SFML libraries and what they are linked to. From the SFML 2.5.1 downloads page, there are several versions of the SFML pre-built libraries available. Make sure you download and reference the version that matches your compiler (e.g. MinGW, Visual C++ 12, Visual C++ 15, etc.).
On Windows, several different compilers are supported; so for example, if you are building your project with Visual Studio 15, be sure you've downloaded and referenced the Visual C++ 15 SFML libraries. Also be sure the SFML libraries match the architecture of your compiler (i.e. 32-bit or 64-bit).
If you don't see the compiler you're using in the list offered on the SFML downloads page, you can download the SFML source and build it yourself using your compiler. This will help ensure the SFML libraries have binary compatibility with your project. Or, you can browse through older versions of SFML pre-built libraries to see if any of those match the compiler you're using for your project.