Weirdly unable to compile SDL2 with c++, vcpkg, cmake and mingw?

I’ve been trying to use cmake to compile my code and linking to SDL2 installed with vcpkg, but it just won’t work.

When running cmake --build build I get the errors cannot find -lSDL2main: No such file or directory and cannot find -lSDL2: No such file or directory. I’m unsure how to fix it since the headers work in my main.cpp and I can access the functions but it won’t compile.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.19)

project(atomer)
set(CMAKE_TOOLCHAIN_FILE $ENV{VCPKG_ROOT}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file")
set(CMAKE_CXX_STANDARD 17)
find_package(fmt CONFIG REQUIRED)
find_package(SDL2 CONFIG REQUIRED)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2main)
target_link_libraries(${PROJECT_NAME} PUBLIC mingw32 SDL2main SDL2)

src/main.cpp:

#include <SDL2/SDL.h>
#include <iostream>
using namespace std;

SDL_Window* init_window() {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Window* win = SDL_CreateWindow(
		"atomer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 500, 0
	);

	return win;
}

int main() {
	init_window();
	return 0;
}

The two error lines:

C:/ProgramData/mingw64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2main: No such file or directory
C:/ProgramData/mingw64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2: No such file or directory

Directory:


I’ve followed some examples that have used this combination of tools (SDL with vcpkg and cmake) and it works well for them.
If anyone who could give me insight into what is going wrong or what I’m supposed to do to fix this it would be very appreciated! :slightly_smiling_face:

Remove this line. You’re already linking to SDL2 using the 2 lines above it.