Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 renamed function. But somehow that is not happening?

I'm running Leopard.

Note that this is a different issue from question 550455.

like image 437
Bemmu Avatar asked Feb 24 '10 22:02

Bemmu


3 Answers

You can also use the provided tool by SDL sdl-config:

gcc sdltest.c -o sdltest `sdl-config --cflags --libs`
like image 61
Gato Avatar answered Nov 15 '22 19:11

Gato


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"
like image 44
Bemmu Avatar answered Nov 15 '22 18:11

Bemmu


Config: OSX 10.10.5 XCODE 7.0 SDL 1.2.15

How to reproduce:

  1. Copied the SDL.Framework to /Library/Frameworks as explained on the readme.txt

  2. On XCODE Template > Building settings > Framework Search path as /Library/Frameworks

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

like image 1
Sergio Garcia Avatar answered Nov 15 '22 17:11

Sergio Garcia