I tried to build a c++ project that uses sdl2 with premake5 as my buildsystem. My goal was simply to get a window running, using the SDL2 code, provided by lazyfoo on SDL2: https://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php
For this, I downloaded the SDL2 source and tried to compile it as a static library as explained on the official premake website: https://premake.github.io/docs/Linking
The source is from https://github.com/libsdl-org/SDL/releases/tag/release-2.30.1 , specifically the SDL2-2.30.1.zip file.
I get an undefined reference error to SDL_INIT, SDL_GetError, SDL_CreateWindow and basically every other SDL function, I am using in my code.
After failing multiple times, I followed this tutorial on github to get a static library working in the first place. https://github.com/premake/premake-core/wiki/Tutorial-Premake-example-with-GLFW-and-OpenGL
I removed all the opengl and glfw code, since I just wanted to get a simple print function working. After I succeeded, I just copied the code and updated it to fit the SDL2 include directories and file paths.
Now I get an undefined reference error as described above.
My premake5.lua looks like this:
workspace "Premake Test"
location "Generated"
language "C++"
architecture "x86_64"
configurations { "Debug", "Release" }
filter "configurations:Debug"
symbols "On"
filter "configurations:Release"
optimize "On"
filter { }
targetdir("Build/Bin/%{prj.name}/%{cfg.longname}")
objdir("Build/Obj/%{prj.name}/%{cfg.longname}")
project "SDL"
kind "StaticLib"
includedirs "libs/SDL2-2.30.1/include"
files "libs/SDL2-2.30.1/src/*"
function useSDL()
includedirs "libs/SDL2-2.30.1/include"
links "SDL"
end
project "TestLib"
kind "StaticLib"
files "libs/testlib/**"
function useExampleLib()
includedirs "libs/testlib"
links "TestLib"
end
project "App"
kind "WindowedApp"
files "src/**"
useSDL()
useExampleLib()
the ExampleLib Project is just my first try to get a general static library working
my main.cpp looks like this:
#include <SDL.h>
#include <iostream>
#include "test.h"
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main( int argc, char* args[] )
{
print("Wenn das klappt, dann WTF!!!");
SDL_Window *window = nullptr;
SDL_Surface *screenSurface = nullptr;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "SDL could not be initialized! Error: " << SDL_GetError() << std::endl;
return 0;
}
window = SDL_CreateWindow( "SDL_Premake_Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if (window == nullptr)
{
std::cout << "SDL window could not be created! Error: " << SDL_GetError() << std::endl;
return 0;
}
screenSurface = SDL_GetWindowSurface(window);
SDL_FillRect( screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
SDL_UpdateWindowSurface(window);
SDL_Event e;
bool quit = false;
while (!quit)
while (SDL_PollEvent(&e))
if (e.type == SDL_QUIT)
quit = true;
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
my directory looks like this:
PREMAKE_TEST/
libs/
SDL2-2.30.1/
include/ (contains all the .h files)
src/ (contains all the .c files)
testlib/
test.cpp
test.h
src/
main.cpp
premake5.lua
to build my project, I first ran premake5 gmake and then make in the "Generate" folder, generated by premake.
The exact error I got after running make looks like this:
$ make
==== Building SDL (debug) ====
==== Building TestLib (debug) ====
==== Building App (debug) ====
Linking App
/usr/bin/ld: ../Build/Obj/App/Debug/main.o: in function `main':
/home/.../dev/premake_test/Generated/../src/main.cpp:18:(.text+0x7e): undefined reference to `SDL_Init'
/usr/bin/ld: /home/.../dev/premake_test/Generated/../src/main.cpp:20:(.text+0xa6): undefined reference to `SDL_GetError'
/usr/bin/ld: /home/.../dev/premake_test/Generated/../src/main.cpp:24:(.text+0xf7): undefined reference to `SDL_CreateWindow'
/usr/bin/ld: /home/.../dev/premake_test/Generated/../src/main.cpp:27:(.text+0x123): undefined reference to `SDL_GetError'
/usr/bin/ld: /home/.../dev/premake_test/Generated/../src/main.cpp:31:(.text+0x156): undefined reference to `SDL_GetWindowSurface'
/usr/bin/ld: /home/.../dev/premake_test/Generated/../src/main.cpp:32:(.text+0x179): undefined reference to `SDL_MapRGB'
/usr/bin/ld: /home/.../dev/premake_test/Generated/../src/main.cpp:32:(.text+0x18c): undefined reference to `SDL_FillRect'
/usr/bin/ld: /home/.../dev/premake_test/Generated/../src/main.cpp:33:(.text+0x198): undefined reference to `SDL_UpdateWindowSurface'
/usr/bin/ld: /home/.../dev/premake_test/Generated/../src/main.cpp:38:(.text+0x1b8): undefined reference to `SDL_PollEvent'
/usr/bin/ld: /home/.../dev/premake_test/Generated/../src/main.cpp:42:(.text+0x1d8): undefined reference to `SDL_DestroyWindow'
/usr/bin/ld: /home/.../dev/premake_test/Generated/../src/main.cpp:43:(.text+0x1dd): undefined reference to `SDL_Quit'
collect2: Fehler: ld gab 1 als Ende-Status zurück
make[1]: *** [App.make:81: ../Build/Bin/App/Debug/App] Fehler 1
make: *** [Makefile:43: App] Fehler 2
In case, this is important, my system is garuda-linux, arch based.
I also thought, the problem might be that the #include SDL.h includes the system SDL but I wasn't able to confirm this.
Also there might be ways to do this using cmake or g++ but I want to find a solution using premake since it seems to be a great and flexible way to build c++ projects in general.
I hope, this post is not a duplicate but every post, I have read so far has not helped me solve my problem.
Thanks in advance for any help :)
EDIT:
the output I got after running make -n
$make -n
echo "==== Building SDL (debug) ===="
make --no-print-directory -C . -f SDL.make config=debug
:
echo "==== Building TestLib (debug) ===="
make --no-print-directory -C . -f TestLib.make config=debug
:
echo "==== Building App (debug) ===="
make --no-print-directory -C . -f App.make config=debug
echo Linking App
g++ -o "../Build/Bin/App/Debug/App" ../Build/Obj/App/Debug/main.o -L/usr/lib64 -m64 ../Build/Bin/SDL/Debug/libSDL.a ../Build/Bin/TestLib/Debug/libTestLib.a
result of make -n after make clean
make -n
echo "==== Building SDL (debug) ===="
make --no-print-directory -C . -f SDL.make config=debug
echo Creating ../Build/Obj/SDL/Debug
mkdir -p ../Build/Obj/SDL/Debug
echo SDL.c
cc -MD -MP -I../libs/SDL2-2.30.1/include -m64 -fPIC -g -o "../Build/Obj/SDL/Debug/SDL.o" -MF "../Build/Obj/SDL/Debug/SDL.d" -c "../libs/SDL2-2.30.1/src/SDL.c"
echo SDL_assert.c
cc -MD -MP -I../libs/SDL2-2.30.1/include -m64 -fPIC -g -o "../Build/Obj/SDL/Debug/SDL_assert.o" -MF "../Build/Obj/SDL/Debug/SDL_assert.d" -c "../libs/SDL2-2.30.1/src/SDL_assert.c"
echo SDL_dataqueue.c
cc -MD -MP -I../libs/SDL2-2.30.1/include -m64 -fPIC -g -o "../Build/Obj/SDL/Debug/SDL_dataqueue.o" -MF "../Build/Obj/SDL/Debug/SDL_dataqueue.d" -c "../libs/SDL2-2.30.1/src/SDL_dataqueue.c"
echo SDL_error.c
cc -MD -MP -I../libs/SDL2-2.30.1/include -m64 -fPIC -g -o "../Build/Obj/SDL/Debug/SDL_error.o" -MF "../Build/Obj/SDL/Debug/SDL_error.d" -c "../libs/SDL2-2.30.1/src/SDL_error.c"
echo SDL_guid.c
cc -MD -MP -I../libs/SDL2-2.30.1/include -m64 -fPIC -g -o "../Build/Obj/SDL/Debug/SDL_guid.o" -MF "../Build/Obj/SDL/Debug/SDL_guid.d" -c "../libs/SDL2-2.30.1/src/SDL_guid.c"
echo SDL_hints.c
cc -MD -MP -I../libs/SDL2-2.30.1/include -m64 -fPIC -g -o "../Build/Obj/SDL/Debug/SDL_hints.o" -MF "../Build/Obj/SDL/Debug/SDL_hints.d" -c "../libs/SDL2-2.30.1/src/SDL_hints.c"
echo SDL_list.c
cc -MD -MP -I../libs/SDL2-2.30.1/include -m64 -fPIC -g -o "../Build/Obj/SDL/Debug/SDL_list.o" -MF "../Build/Obj/SDL/Debug/SDL_list.d" -c "../libs/SDL2-2.30.1/src/SDL_list.c"
echo SDL_log.c
cc -MD -MP -I../libs/SDL2-2.30.1/include -m64 -fPIC -g -o "../Build/Obj/SDL/Debug/SDL_log.o" -MF "../Build/Obj/SDL/Debug/SDL_log.d" -c "../libs/SDL2-2.30.1/src/SDL_log.c"
echo SDL_utils.c
cc -MD -MP -I../libs/SDL2-2.30.1/include -m64 -fPIC -g -o "../Build/Obj/SDL/Debug/SDL_utils.o" -MF "../Build/Obj/SDL/Debug/SDL_utils.d" -c "../libs/SDL2-2.30.1/src/SDL_utils.c"
echo Linking SDL
ar -rcs "../Build/Bin/SDL/Debug/libSDL.a" ../Build/Obj/SDL/Debug/SDL.o ../Build/Obj/SDL/Debug/SDL_assert.o ../Build/Obj/SDL/Debug/SDL_dataqueue.o ../Build/Obj/SDL/Debug/SDL_error.o ../Build/Obj/SDL/Debug/SDL_guid.o ../Build/Obj/SDL/Debug/SDL_hints.o ../Build/Obj/SDL/Debug/SDL_list.o ../Build/Obj/SDL/Debug/SDL_log.o ../Build/Obj/SDL/Debug/SDL_utils.o
:
echo "==== Building TestLib (debug) ===="
make --no-print-directory -C . -f TestLib.make config=debug
echo Creating ../Build/Obj/TestLib/Debug
mkdir -p ../Build/Obj/TestLib/Debug
echo test.cpp
g++ -MD -MP -m64 -fPIC -g -o "../Build/Obj/TestLib/Debug/test.o" -MF "../Build/Obj/TestLib/Debug/test.d" -c "../libs/testlib/test.cpp"
echo Linking TestLib
ar -rcs "../Build/Bin/TestLib/Debug/libTestLib.a" ../Build/Obj/TestLib/Debug/test.o
:
echo "==== Building App (debug) ===="
make --no-print-directory -C . -f App.make config=debug
echo Creating ../Build/Obj/App/Debug
mkdir -p ../Build/Obj/App/Debug
echo main.cpp
g++ -MD -MP -I../libs/SDL2-2.30.1/include -I../libs/testlib -m64 -fPIC -g -o "../Build/Obj/App/Debug/main.o" -MF "../Build/Obj/App/Debug/main.d" -c "../src/main.cpp"
make[1]: *** No rule to make target '../Build/Bin/SDL/Debug/libSDL.a', needed by '../Build/Bin/App/Debug/App'. Stop.
make: *** [Makefile:43: App] Error 2