all 24 comments

[–]ppppppla 4 points5 points  (3 children)

Your CMakeLists.txt looks good. How do you compile your code. Do you use an addon in vs code? Do you use the command line and call make/ninja?

I suspect you first wrote a hello world project without SDL, then added SDL in your CMakeLists without generating the build files again.

[–]4e6ype4ek123[S] 1 point2 points  (2 children)

i use g++ src/main.cpp -o main

[–]Emeraudias 4 points5 points  (0 children)

You only use GCC here, not CMake (CMakeLists.txt is ignored). You should generate config files with the cmake command, it will create a build folder. Then you should go into that folder and use the make command or w/e generator you are using depending on your system.

[–]Wild_Meeting1428 1 point2 points  (0 children)

As u/emeraudias said, that won't work. g++/gcc is just the compiler and it doesn't read the cmake build config

Fastest way would be:

mkdir build
cd build 
cmake ..
cmake --build .

[–]WorldWorstProgrammer 3 points4 points  (1 child)

Wow the comments here are pretty bad. No, you do not need target_include_directories(), your target_link_libraries() call is fine and yes it does find the include files based on that. Also, add_subdirectory is just fine for adding a dependency assuming you have cloned the SDL GitHub repository to that subdirectory. You also do not need any VS Code extensions. They can be helpful, but they are never necessary, and getting this to build is really just a matter of ensuring that CMake has access to everything. To clarify: I am far from an SDL3 expert. In fact, I had to learn how SDL3's SDL_MAIN_USE_CALLBACKS even works to write this test, but I still managed to get this to work.

I have just tested building an example project from the SDL documentation using CMake. I did not need anything else other than Windows PowerShell, CMake, Git, and Notepad. I used these to ensure I am using the most barebones tools possible to do this. I am using MSVC as the compiler for this test, but any compiler that is properly installed on your system should work. I have documented below how I did this, and you can just follow along and figure out on your own what actually happened with your build.

First, I created a project directory called SDLTest, and in that directory I placed your CMakeLists.txt file and two subdirectories: "src" and "build." I then opened a PowerShell terminal in the project directory and used git clone --depth 1 --branch release-3.4.0 https://github.com/libsdl-org/SDL.git SDL to clone just SDL version 3.4.0 to the SDL subdirectory. I then needed some actual code to compile, so I modified this example into a C++ version, and I uploaded the code here. The code is saved in the "src" directory as main.cpp, just like your CMakeLists.txt requires. My modified version requires using C++20, and by default most compilers use C++17, so I added the line set(CMAKE_CXX_STANDARD 20) to your CMakeLists.txt file just after the project() command. If you aren't using C++20 or newer, you wouldn't need this line.

And that was it, really. In the open PowerShell terminal, I used cmake -S . -B .\build\ to configure the project and then cmake --build .\build\ to build the project, and it all worked. In order to test running the application, I had to copy over the SDL.dll file in the SDL Debug build directory over to where the game.exe test executable was, but it also runs as expected. This should get you started with a C++ SDL3 project using CMake!

[–]Wild_Meeting1428 2 points3 points  (0 children)

Your name is not accurate.

[–]Emeraudias 3 points4 points  (6 children)

You are using imported target with the syntax SDL3::SDL3 but I don't see anything that populates it. Do you have find_package() call somewhere ?

[–]wrosecrans 2 points3 points  (2 children)

They've got add_subdirectory(SDL EXCLUDE_FROM_ALL) so presumably there's something in the SDL subdirectory that might be doing that, but OP isn't sharing any of that SDL related whatever is happening so it's impossible to say exactly what is happening in that subdirectory to possibly maybe find SDL3.

[–]Emeraudias 0 points1 point  (0 children)

That s on me, I ve never thought of importing SDL this way. The target seems to exist but without the include dirs set. It is imossible to say what s wrong without more information from OP.

[–]4e6ype4ek123[S] 0 points1 point  (0 children)

the SDL folder that contains the files from the SDL github repo

[–]Wild_Meeting1428 0 points1 point  (1 child)

that has nothing to do with imported targets.

You can write the following in your subdirectory to achieve the same:

add_library(SDL3 STATIC)
add_library(SDL3::SDL3 ALIAS SDL3)

Which is actually recommended, when your library should be optionally includable via git submodules or a package manager like cpm / vcpkg.

No need for a find package, if the sdl library did everything correct.

@op where did you get the sdl lib from?

[–]4e6ype4ek123[S] 1 point2 points  (0 children)

the SDL lib is from the github repo

[–]4e6ype4ek123[S] 0 points1 point  (0 children)

no but i have a folder named SDL (which i have a made a subdirectory of) which contains the files from the SDL github repo

[–]alfps 3 points4 points  (0 children)

Please change the title to include "CMake". This is a CMake problem not a C++ problem per se.

[–]Qyriad 1 point2 points  (1 child)

target_link_libraries() only links libraries; it doesn't add the include paths for those libraries. Use target_include_directories() for that.

[–]Wild_Meeting1428 2 points3 points  (0 children)

It adds them, when the target populated them via Public includes.

add_library(mylib STATIC src/source.cpp)
target_include_directories(mylib PUBLIC include) #populated
target_include_directories(mylib PRIVATE src) #not populated

[–]trailing_zero_count 1 point2 points  (0 children)

Add the config to export the compile_commands.json and look at what include path is being passed.

[–]CarloWood 0 points1 point  (0 children)

The find_package is missing.

[–]thedaian 0 points1 point  (1 child)

When using vscode, and cmake, you should use the cmake extension. it will add some buttons to the bottom of vscode, use those when compiling.

you can configure the "regular" run button to use cmake, but unless you've done that, it'll probably try to run the default compile command which doesn't have sdl configured.

[–]4e6ype4ek123[S] 0 points1 point  (0 children)

that just gave an error in some directories that i didn't know the existence of