undefined reference to `SDL_main'

Started by
15 comments, last by georger.araujo 9 years, 12 months ago
Are you trying to link it statically or dynamically?

Dynamically.

g++ -o Main.o -lmingw32 -LD:\SDL2\i686-w64-mingw32\lib\libSDL2main.a -LD:\SDL2\i686-w64-mingw32\lib\libSDL2.a -mwindows

I got "undefined reference to `WinMain@16'"

Advertisement

Allright, I'm stupid. What happens if you rename your main function as WinMain ? (with the command I posted on my previous reply)

It returned the same error.

You could issue the command mingw32-make install in your SDL2 root directory...

Guess I can't think of anything that could cause this... I really can't see the problem.

It could be several things...

  • Your MinGW architecture may differ from the .a files (probably not your problem)
    • Try changing the directory from i686-w64-mingw32 to x86_64-w64-mingw32
  • You may be missing the sdl-config (shouldn't bring errors)
    • Adding `sdl2-config --libs` or `sdl2-config --static-libs`
  • Missing flags, with which I can't help much since I link with -lSDL2 in linux, nothing more...

I'm using MinGW/Msys and SDL2.
---------------------------------------
These are my console contents.
g++ -c Main.cpp -ID:\SDL2\i686-w64-mingw32\include
g++ -o Main.o -LD:\SDL2\i686-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2
D:\SDL2\i686-w64-mingw32\lib/libSDL2main.a(SDL_windows_main.o): In function `console_main':
/Users/slouken/release/SDL/SDL2-2.0.2-source/foo-x86/../src/main/windows/SDL_windows_main.c:140: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status
make: *** [Main.exe] Error 1
[Finished in 0.6s]
--------------------------------------
Here is Main.cpp.

#include <SDL2/SDL.h>

#include <iostream>

int main(int argc, char* argv[]) {
	const char* test = "Test";

	std::cout << test << std::endl;

	return 0;
}

This is my first time using SDL2 instead of SDL 1.2.

I couldn't get your code to work with a freshly downloaded MinGW and SDL 2.0.3:

g++.exe -Wall -m32 -g -IC:\SDL2\i686-w64-mingw32\include\SDL2 -c main.cpp -o main.o
In file included from C:\SDL2\i686-w64-mingw32\include\SDL2/SDL_config.h:25:0,
from C:\SDL2\i686-w64-mingw32\include\SDL2/SDL_stdinc.h:31,
from C:\SDL2\i686-w64-mingw32\include\SDL2/SDL_main.h:25,
from C:\SDL2\i686-w64-mingw32\include\SDL2/SDL.h:67,
from main.cpp:1:
C:\SDL2\i686-w64-mingw32\include\SDL2/SDL_platform.h:121:26: fatal error: winapifamily.h: No such file or directory
#include <winapifamily.h>
^
compilation terminated.

On TDM-GCC (which bundles "a few patches for Windows-friendliness, and the free and open-source MinGW or MinGW-w64 runtime APIs"), it works fine:

g++.exe -Wall -m32 -g -IC:\SDL2\i686-w64-mingw32\include\SDL2 -c main.cpp -o main.o
g++.exe -LC:\SDL2\i686-w64-mingw32\lib -o test.exe main.o -m32 -lmingw32 -lSDL2main -lSDL2.dll -luser32 -lgdi32 -lwinmm -ldxguid

Consider giving TDM-GCC a try.

By the way, if you're going to stick with GCC on Windows, I've written instructions on how to set up Code::Blocks with SDL2.

EDIT: You'll notice I used a different path for my SDL2 installation (C:\SDL2). Also, I changed your code to


#include <SDL.h>

#include <iostream>

int main( int argc, char * argv[] )
{
    const char * test = "Test";

    std::cout << test << std::endl;

    return 0;
}

Basically, I changed #include <SDL2/SDL.h> to #include <SDL.h>.

While these two changes are not major, and are unrelated to the root cause of the problem, they still could cause some confusion.

Let me give this a shot.

When doing the linking step, you use:

g++ -o Main.o -LD:\SDL2\i686-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2

This looks like you want to link nothing to the libs, and output it to a file called Main.o. Try changing it to:

g++ Main.o -LD:\SDL2\i686-w64-mingw32\lib -lmingw32 -lSDL2main -lSDL2

instead. This means you want to link Main.o to the libs, and output it to "a.exe"

UPDATE.

I just tested this with TDM-GCC's TDM32 bundle, and it didn't work. I investigated and found out that i686-w64-mingw32\include\SDL2\SDL_platform.h changed between SDL 2.0.2 and SDL 2.0.3 in a way that any Windows compiler other than a MinGW-w64 based one, or MSVC, would probably not work.

I didn't notice this before because I use TDM-GCC's TDM64 bundle (along with a separate download of 32-bit GDB) to build 32-bit binaries (notice that I use the -m32 flag in my examples); this being a a MinGW-w64 derivative, my toolchain was one of the "intended" ones, and worked fine.

So, it looks like the way to go as of now for using SDL2 on Windows with GCC is to use a MinGW-w64 derivative such as TDM-GCC's TDM64 bundle.

This looks like it was already fixed upstream, but as far as 2.0.3 is concerned, this is the state of affairs.

This topic is closed to new replies.

Advertisement