Recently I have started to learn CMake. To practice, I am trying to link SDL2 manually. I know that there is another way around using find_file
which is easy. But I want to do it myself for practice.
I get an error when I try to link the libSDL2main.a
file (running the Makefile using cmd mingw32-make)
[ 50%] Linking CXX executable exe0.exe
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -llibSDL2main
collect2.exe: error: ld returned 1 exit status
CMakeFiles\exe0.dir\build.make:105: recipe for target 'exe0.exe' failed
mingw32-make[2]: *** [exe0.exe] Error 1
CMakeFiles\Makefile2:94: recipe for target 'CMakeFiles/exe0.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/exe0.dir/all] Error 2
Makefile:102: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(SDL_Test_Project)
include_directories(include)
add_executable(exe0 main.cpp)
target_link_libraries(exe0 libSDL2main.a)
Here main.cpp is only a source file. I have put SDL2.dll
and libSDL2main.a
into the root of the project directory. (I used the CMake GUI to generate the Makefile in Windows 10).
If you want to link to the SDL2 libraries directly in target_link_libraries()
(without defining IMPORTED
targets, or using find_library()
), use the full path to each library. The CMAKE_SOURCE_DIR
variable provides the full path to the root directory of the CMake project:
target_link_libraries(exe0 PRIVATE
mingw32
${CMAKE_SOURCE_DIR}/libSDL2main.a
${CMAKE_SOURCE_DIR}/SDL2.dll
)
Note, for SLD2, you may also have to add the mingw32
to this command when using MinGW for compilation.