OK I made some more progress. Basically the original issue was because I was not linking the target against SFML dependencies (which I missed, although described in
https://www.sfml-dev.org/tutorials/2.6/start-vc.php)
Here is the issue I have now - I built a
static version of SFML myself. I took an SFML toy example, but got a lot of link errors when building my application.
Here are the steps I followed (and how I fixed it)
- Downloaded SFML 2.6.0 source from https://www.sfml-dev.org/files/SFML-2.6.0-sources.zip
- Use Cmake GUI and Visual Studio 2019 to build SFML
Where is the source code: C:/SFML-2.6.0-sources/SFML-2.6.0
Where to build the libraries: C:/SFML_static_2.6.0
Click on Configure. Uncheck BUILD_SHARED_LIBS, and check SFML_USE_STATIC_STD_LIBS. Click on Configure again. Click on Generate. This generates a visual studio solution SFML.sln. Open it and build the release version of SFML. This builds a static version of SFML in C:\SFML_static_2.6.0\lib\Release
- Edit SFMLConfig.cmake in 'C:\SFML_static_2.6.0\SFMLConfig.cmake' (where SFML gets built)
Add the paths lib/Release and the paths to SFML dependencies. To FIND_SFML_PATHS , add
"${CMAKE_CURRENT_LIST_DIR}/lib/Release" (where SFML libraries are built)
"C:/SFML-2.6.0-sources/SFML-2.6.0/extlibs/libs-msvc-universal/x64" (SFML dependencies shipped with SFML source)
- Create a toy example. I created main.cpp with the code in https://www.sfml-dev.org/tutorials/2.6/start-vc.php
In the same location, create CMakeLists.txt with the following content (code taken from https://en.sfml-dev.org/forums/index.php?topic=24070.0)
cmake_minimum_required(VERSION 3.23)
project(SFMLTest)
## If you want to link SFML statically
set(SFML_STATIC_LIBRARIES TRUE)
## In most cases better set in the CMake cache
set(SFML_DIR "C:/SFML_static_2.6.0")
find_package(SFML 2.6 COMPONENTS graphics audio REQUIRED)
add_executable(SFMLTest main.cpp)
target_link_libraries(SFMLTest sfml-graphics sfml-audio)
- Generate VS 2019 solution
cmake -G "Visual Studio 16 2019" -A x64 -B build
- Open SFMLTest.sln in 'build' folder, and Build the solution.
The last step gives a lot of link errors. Eg:-
Error LNK2038 mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' in main.obj SFMLTest sfml-graphics-s.lib(Color.obj)
To fix this, I did the following.
Right click SFMLTest, click on properties.
C/C++ > Code Generation > Runtime Library is set to 'Multi-threaded DLL (/MD)'.
I changed it to 'Multi-threaded (/MT)' .
What I do not understand is, if I use the the pre-built version of SFML for VS 2019 from
https://www.sfml-dev.org/files/SFML-2.6.0-windows-vc16-64-bit.zip, it works out of the box. And interestingly, when I check the solution generated with this, I still have Multi-threaded DLL (/MD), but it works. I am curious about why this works.
So I can't quite figure out what I am doing wrong. Any help is appreciated.