Jump to content

  • Log In with Google      Sign In   
  • Create Account

undefined reference to `SDL_main'


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
16 replies to this topic

#1 austinsheep   Members   -  Reputation: 180

Like
0Likes
Like

Posted 20 April 2014 - 03:43 AM

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.


Edited by austinsheep, 20 April 2014 - 04:04 AM.


#2 nfactorial   Members   -  Reputation: 735

Like
0Likes
Like

Posted 20 April 2014 - 05:44 AM

Sounds like you're not linking to the appropriate library.

 

n!



#3 DareDeveloper   GDNet+   -  Reputation: 1018

Like
2Likes
Like

Posted 20 April 2014 - 07:08 AM

I think that is a classic ... I have struggled with that error message several times as well, but that was a long time ago.

 

Maybe those responses help:

http://stackoverflow.com/questions/115813/how-to-statically-compile-an-sdl-game-on-windows

 

First thing I thought was "isn't -mwindows important as well"!? ... but not sure if that is it.

I also think it is a linking problem.

 

 

EDIT:

I think ... (but I am not 100% sure) the last time I fought with this error it was because of a 32-bit vs 64-bit problem. When I used the other libraries it worked as far as I remember ... even if it did not really make sense to me at the time.


Edited by DareDeveloper, 20 April 2014 - 07:29 AM.

Given enough eyeballs, all mysteries are shallow.

MeAndVR


#4 dejaime   Crossbones+   -  Reputation: 4157

Like
0Likes
Like

Posted 20 April 2014 - 09:20 AM

Try to compile without -lSDL2main ...

Also, try to swap it with -lSDL2, the order is important when linking.

 

How did you install SDL2? You may have to link the lib files with their full paths (e.g. -L/PATH/TO/FILE/libSDL2.a )


Edited by dejaime, 20 April 2014 - 11:23 AM.


#5 austinsheep   Members   -  Reputation: 180

Like
0Likes
Like

Posted 20 April 2014 - 06:26 PM

Try to compile without -lSDL2main ...

Also, try to swap it with -lSDL2, the order is important when linking.

 

How did you install SDL2? You may have to link the lib files with their full paths (e.g. -L/PATH/TO/FILE/libSDL2.a )

When I compile it without -lSDL2main it returns the error "undefined reference to `WinMain@16'"

 

When I swap it, it returns an even longer error message. (including "undefined reference to `SDL_main'")

 

Didn't I link them correctly? They're on my D: drive.

 

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


#6 austinsheep   Members   -  Reputation: 180

Like
0Likes
Like

Posted 20 April 2014 - 07:38 PM

I just tried compiling SDL 1.2 and it returned the same error



#7 dejaime   Crossbones+   -  Reputation: 4157

Like
0Likes
Like

Posted 20 April 2014 - 08:02 PM

Try

g++ -c Main.cpp -ID:\SDL2\i686-w64-mingw32\include
g++ -o Main.o -LD:\SDL2\i686-w64-mingw32\lib -lmingw32 -lSDL2 -lSDL2main -mwindows

Linkage problems are hard because they aren't obvious and, without seeing all the details, it is hard to know what is and what isn't missing. You can also try sdl-config with --libs or --static-libs as: <`sdl-config --libs`> without < and >...


Edited by dejaime, 20 April 2014 - 08:23 PM.


#8 Alundra   Members   -  Reputation: 1990

Like
1Likes
Like

Posted 20 April 2014 - 08:05 PM

SDL uses a macro for main, you have to link the SDL2main lib or "#undef main" after include SDL.h.


Edited by Alundra, 20 April 2014 - 08:07 PM.


#9 austinsheep   Members   -  Reputation: 180

Like
0Likes
Like

Posted 20 April 2014 - 08:18 PM

Try

g++ -c Main.cpp -ID:\SDL2\i686-w64-mingw32\include
g++ -o Main.o -LD:\SDL2\i686-w64-mingw32\lib -lmingw32 -lSDL2 -lSDL2main -mwindows

It returned the same error as to when I switched -lSDL2 and -lSDL2main

 

SDL uses a macro for main, you have to link the SDL2main lib or "#undef main" after include SDL.h.

 

 

When I try statically linking it, I get  "undefined reference to `WinMain@16'"

 

When I added "#undef main" after include SDL.h, I get "undefined reference to 'SDL_main'"

 

I know it's something I'm doing wrong, I'm just not sure what.  I'm sorry this is is being so complicated.



#10 dejaime   Crossbones+   -  Reputation: 4157

Like
0Likes
Like

Posted 20 April 2014 - 08:42 PM

The problem with linkage is that we can only guess without knowing the details of the system where you are compiling.

Are you trying to link it statically or dynamically?

 

Try

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


Edited by dejaime, 20 April 2014 - 08:43 PM.


#11 austinsheep   Members   -  Reputation: 180

Like
0Likes
Like

Posted 20 April 2014 - 08:44 PM

Are you trying to link it statically or dynamically?

 

Dynamically.

 

g++ -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'"


Edited by austinsheep, 20 April 2014 - 08:45 PM.


#12 dejaime   Crossbones+   -  Reputation: 4157

Like
0Likes
Like

Posted 20 April 2014 - 08:53 PM

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


Edited by dejaime, 20 April 2014 - 08:54 PM.


#13 austinsheep   Members   -  Reputation: 180

Like
0Likes
Like

Posted 20 April 2014 - 09:00 PM

It returned the same error.



#14 dejaime   Crossbones+   -  Reputation: 4157

Like
0Likes
Like

Posted 20 April 2014 - 09:53 PM

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...

Edited by dejaime, 20 April 2014 - 10:38 PM.


#15 georger.araujo   Members   -  Reputation: 1205

Like
0Likes
Like

Posted 21 April 2014 - 08:05 AM

 

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.


Edited by georger.araujo, 21 April 2014 - 08:13 AM.


#16 ultramailman   Prime Members   -  Reputation: 1720

Like
2Likes
Like

Posted 21 April 2014 - 01:59 PM

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"


Edited by ultramailman, 21 April 2014 - 02:00 PM.


#17 georger.araujo   Members   -  Reputation: 1205

Like
0Likes
Like

Posted 21 April 2014 - 02:27 PM

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.






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS