SDL causes Undefined symbols: "_main", referenced from: start in crt1.10.5.o
When I try to use SDL in my c++ program, I get the following:
> g++ minimal.cpp SDLMain.m
Undefined symbols:
"_main", referenced from:
start in crt1.10.5.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Here's my minimal.cpp:
#include <SDL/SDL.h>
int main(int argc, char **argv) {
return 0;
}
What I could gather from http://www.libsdl.org/faq.php?action=listentries&category=7 was that by including SDL.h, it renames my main function through some macro magic. But then SDLMain.m is supposed to make things right again by calling that re开发者_如何学JAVAnamed function. But somehow that is not happening?
I'm running Leopard.
Note that this is a different issue from question 550455.
You can also use the provided tool by SDL sdl-config:
gcc sdltest.c -o sdltest `sdl-config --cflags --libs`
Solution was to use the SDLMain.m file included in SDL-devel-1.2.14-extras.dmg from the SDL homepage. For some reason the one I was using before had mysteriously stopped working. Here's my working compile command:
g++ -framework SDL -framework Cocoa -I/usr/local/include/SDL/ minimal.cpp "/Library/Application Support/Developer/Shared/Xcode/Project Templates/SDL Application/SDLMain.m"
Config: OSX 10.10.5 XCODE 7.0 SDL 1.2.15
How to reproduce:
Copied the SDL.Framework to /Library/Frameworks as explained on the readme.txt
On XCODE Template > Building settings > Framework Search path as /Library/Frameworks
I included the SDL framework on the main.cpp file as follows:
#include <SDL/SDL.h>
Then I had the same problem loading SDL Framework on XCODE, because of a double declaration of class main that blocked the compilation.
This is the error message:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
(maybe you meant: _SDL_main)
This how I solved it:
I checked and saw that SDL.h is including all the following files:
#include "SDL_main.h"
#include "SDL_stdinc.h"
#include "SDL_audio.h"
#include "SDL_cdrom.h"
#include "SDL_cpuinfo.h"
#include "SDL_endian.h"
#include "SDL_error.h"
#include "SDL_events.h"
#include "SDL_loadso.h"
#include "SDL_mutex.h"
#include "SDL_rwops.h"
#include "SDL_thread.h"
#include "SDL_timer.h"
#include "SDL_video.h"
#include "SDL_version.h"
#include "begin_code.h"
One of them is SDL_main.h and on that file we see:
#define main SDL_main
This line is producing a conflict with the class main on main.cpp, commenting that line on SDL_main.h or commenting the line #include "SDL_main.h" on SDL.h solves the issue. I'm a noob on C++ (I just learn it at University many years ago) but from other languages I know that "hacking" a library is a very bad practice... though it seems to be a particular compatibility problem with MAXOSX and I really want to use XCODE...
Please correct and comment, justify yes or no, as I'm on a learning process.
Cheers!
精彩评论