0

I am trying to compile simple code :

#include <iostream>
#include <SDL2/SDL.h>

int main(int argc, char *argv[])
{

}

but it gives me a lot of bloody errors:

    ||=== Build: Debug in aa (compiler: GNU GCC Compiler) ===|
E:\SDL2\SDL2-2.0.12\i686-w64-mingw32\lib\libSDL2main.a(SDL_windows_main.o)||In function `main_getcmdline':|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|55|undefined reference to `SDL_calloc'|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|60|undefined reference to `SDL_wcslen'|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|60|undefined reference to `SDL_iconv_string'|
E:\SDL2\SDL2-2.0.12\i686-w64-mingw32\lib\libSDL2main.a(SDL_windows_main.o)||In function `OutOfMemory':|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|28|undefined reference to `SDL_ShowSimpleMessageBox'|
E:\SDL2\SDL2-2.0.12\i686-w64-mingw32\lib\libSDL2main.a(SDL_windows_main.o)||In function `main_getcmdline':|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|68|undefined reference to `SDL_SetMainReady'|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|75|undefined reference to `SDL_free'|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|77|undefined reference to `SDL_free'|
||error: ld returned 1 exit status|
||=== Build failed: 8 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

I don't understand why it would give me undefines to random functions when the library itself seems to be working, but alas.

The things i've done as of now are

  • add -lmingw32 -lSDL2 -lSDL2main to linker settings-> other linker options
  • added the include folder for the 32 bit version to search directories-> compiler
  • added the lib folder for the 32 bit version to search directories-> linker
  • added the SDL2.dll to the bin folder of my compiler (i have mingw separately from codeblocks)
genpfault
  • 49,394
  • 10
  • 79
  • 128
Menelaus
  • 11
  • 3
  • Refer to the linked thread, to the section called "undefined reference to various functions". Please tell us if it won't work. – HolyBlackCat Oct 21 '20 at 20:06
  • Hint: your flags are in the wrong order. – HolyBlackCat Oct 21 '20 at 20:43
  • @HolyBlackCat i've set it to -lmingw32 -lSDL2main -lSDL2 and it gives me more errors – Menelaus Oct 22 '20 at 13:47
  • @HolyBlackCat i've really tried many things including the article but it just won't work – Menelaus Oct 22 '20 at 13:54
  • `-lmingw32 -lSDL2main -lSDL2` is the correct order, use it. Please share the new error messages. – HolyBlackCat Oct 22 '20 at 17:20
  • @HolyBlackCat yup! here it is – Menelaus Oct 22 '20 at 18:31
  • http://www.dontpad.com/SDL2 – Menelaus Oct 22 '20 at 18:33
  • What does it show in the "Build log" tab? I'm specifically interested in the compiler flags that will be shown there. The error look like something you'd get if you attempted to link SDL2 statically instead of dynamically (but without adding all flags needed for that). Are you linking statically on purpose, or you don't know what that means? – HolyBlackCat Oct 22 '20 at 18:36
  • `i686-w64-mingw32-g++.exe -LE:\SDL2\SDL2-2.0.12\i686-w64-mingw32\lib -LE:\SDL2\SDL2-2.0.12\x86_64-w64-mingw32\lib -o bin\Debug\SDL2.exe obj\Debug\main.o -static -m32 -lmingw32 -lSDL2main -lSDL2 E:\SDL2\SDL2-2.0.12\i686-w64-mingw32\lib\libSDL2.a(SDL_hidapi.o` No, i don't know what static means in terms of compiling – Menelaus Oct 22 '20 at 18:42
  • Then remove the `-static` flag. – HolyBlackCat Oct 22 '20 at 18:48
  • @HolyBlackCat but that's the thing, i don't understand why it appears because i've set absolutely no `-static flags` – Menelaus Oct 22 '20 at 18:52
  • It's somewhere in your compiler settings, or in the project settings. It's been a while since I used CodeBlocks, so I don't remember where exactly the settings are, but you've enabled this flag somewhere. – HolyBlackCat Oct 22 '20 at 18:56
  • @HolyBlackCat yes! it works now! thank you very much! if it doesn't bother you, could you tell me how you realised i was statically linking? – Menelaus Oct 22 '20 at 19:06
  • Ok, in short SDL itself uses several other libraries. If you link dynamically, then those libraries are already embedded in `SDL2.dll` and you don't need to worry about them. But if you link statically, you have to manually add every library that SDL2 uses to the linker flags. The errors you got don't mention SDL, so they must come from some other library. You could get those errors if you tried to use some other library without the appropriate linker flags (which seemed unlikely), or if you linked statically. – HolyBlackCat Oct 22 '20 at 19:16

1 Answers1

0

Your linker and compiler flags might be wrong. To get the correct compiler flags use:

pkg-config --cflags sdl2

To get the needed link flags:

pkg-config --libs sdl2

If you don't have pkg-config, then you can use the sdl2-config tool that comes with SDL:

sdl2-config --cflags

and:

sdl2-config --libs

Normally, you would add these commands to your build system. I don't know how CodeBlocks works though and if it can do that. Maybe you can add these commands as "flags" but in backticks, as described here:

http://forums.codeblocks.org/index.php?topic=21561.0

Nikos C.
  • 49,123
  • 9
  • 66
  • 95