0

I'm using Visual Studio 2019 to make a small c++ game engine. I'm trying to link SDL2 with my project, and I got it to load the header files, but when I try to build it, I get an error. I'm using CMake btw.

LNK1104 cannot open file 'SDL2main.lib'

My project structure is as follows.

MyGame
    -engine
    -game
    -sdl2
        -include
        -lib

Here is game/CMakeLists.txt

cmake_minimum_required (VERSION 3.8)

add_executable (game 
    main.cpp
)

include_directories(${CMAKE_SOURCE_DIR}/engine)

target_link_libraries(game engine)

Here is my engine/CMakeLists.txt:

cmake_minimum_required (VERSION 3.8)

add_library(engine engine.h app.cpp app.h)

set(SDL2_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/sdl2/include)
set(SDL2_LIB_DIR ${CMAKE_SOURCE_DIR}/sdl2/lib/x86)

include_directories(${SDL2_INCLUDE_DIR})
link_directories(${SDL2_LIB_DIR})

target_link_libraries(engine SDL2main SDL2)

Edit: I found a solution: I moved link_directories to the project’s global CMakeList and added a command to copy the SDL2.dll to the output directory.

alexover1
  • 71
  • 1
  • 3
  • https://cmake.org/cmake/help/latest/module/FindSDL.html might make your life easier – Alan Birtles May 05 '22 at 17:41
  • The command [link_directories](https://cmake.org/cmake/help/latest/command/link_directories.html) doesn't affect on already created targets: https://stackoverflow.com/a/40554704/3440745. – Tsyvarev May 05 '22 at 18:29

0 Answers0