• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
monkey7461

undefined reference to `SDL_main'

16 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

Edited by Alundra
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

 

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
0

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0