Dev C++ Linking Issues

When I try to compile even a simple program with SDL, I get the error

[Linker error] undefined reference to `SDL_main’

I think I have my linker options set right: -lmingw32 -lSDLmain -lSDL

As a test, I’m just trying to compile this

#include <SDL/SDL.h>

#include
#include

using namespace std;

int main()
{
SDL_Surface *screen;

if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf(“Unable to initialize SDL: %s\n”, SDL_GetError());
exit(EXIT_FAILURE);
}
atexit(SDL_Quit);

screen = SDL_SetVideoMode(640, 480, 32, 0);
if (screen == NULL) {
printf(“Unable to set video mode: %s\n”, SDL_GetError());
exit(EXIT_FAILURE);
}

SDL_Delay(2000);

exit(EXIT_SUCCESS);
}

From what I’ve seen on Google, others have had this problem too, but I have
yet to find a solution.

  1. It has to be int main(int argc, char *argv[]), not int main().
    SDL expects, nay, demands it take those two parameters.

  2. If your main is in a C++ file, chances are your ‘main’ function is not
    really called ‘main’ as far as the linker’s concerned, but
    1502347xcgxf532189_main@ or some other garbage. C++ mangles the names to
    prevent overloaded functions from having the same names.

Fortunately, there’s a way to tell C++ to do that. Try:
extern “C” int main(int argc, char *argv[])
instead of
int main()

and tell me how it goes :)On September 29, 2005 08:03 pm, Tim Swast wrote:

When I try to compile even a simple program with SDL, I get the error

[Linker error] undefined reference to `SDL_main’

I think I have my linker options set right: -lmingw32 -lSDLmain -lSDL

As a test, I’m just trying to compile this

#include <SDL/SDL.h>

#include
#include

using namespace std;

int main()
{
SDL_Surface *screen;

if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf(“Unable to initialize SDL: %s\n”, SDL_GetError());
exit(EXIT_FAILURE);
}
atexit(SDL_Quit);

screen = SDL_SetVideoMode(640, 480, 32, 0);
if (screen == NULL) {
printf(“Unable to set video mode: %s\n”, SDL_GetError());
exit(EXIT_FAILURE);
}

SDL_Delay(2000);

exit(EXIT_SUCCESS);
}

From what I’ve seen on Google, others have had this problem too, but I
have yet to find a solution.

this must be a VC++ thing then because linux doesnt require either of these
-mikeOn Thursday 29 September 2005 10:37 pm, Tyler Montbriand wrote:

On September 29, 2005 08:03 pm, Tim Swast wrote:

When I try to compile even a simple program with SDL, I get the error

[Linker error] undefined reference to `SDL_main’

  1. It has to be int main(int argc, char *argv[]), not int main().
    SDL expects, nay, demands it take those two parameters.

  2. If your main is in a C++ file, chances are your ‘main’ function is not
    really called ‘main’ as far as the linker’s concerned, but
    1502347xcgxf532189_main@ or some other garbage. C++ mangles the names to
    prevent overloaded functions from having the same names.

Fortunately, there’s a way to tell C++ to do that. Try:
extern “C” int main(int argc, char *argv[])
instead of
int main()

SDL_main is there to convert things like:

int WinMain(HINSTANCE hInse, HINSTANCE hPrev,LPSTR lpCmdLine,int nCmdShow)
into
int main(int argc, char *argv[])

As well as do initialization that MUST be done before main is called, such as
the crazyass MacOS X carbon/cocoa/aqua/name-of-the-week stuff. It’s a bit
convoluted. The “main”-equivalent that gets called first is inside
-lsdlmain, which in turn calls your main, which may have been renamed to
SDL_main with a preprocessor definition.

linux, and UNIX in general, doesn’t need need an -lSDLmain – they have a
standards-compliant int main(int argc, char *argv[]) interface already and
don’t have crazyass stuff to be initialized before main. Hence, no
overloading, and hence, no picky -lsdlmain to bitch about your main not being
the right type of function.On September 29, 2005 08:56 pm, Mike Frysinger wrote:

this must be a VC++ thing then because linux doesnt require either of these

Mike Frysinger wrote:>On Thursday 29 September 2005 10:37 pm, Tyler Montbriand wrote:

On September 29, 2005 08:03 pm, Tim Swast wrote:

When I try to compile even a simple program with SDL, I get the error

[Linker error] undefined reference to `SDL_main’

  1. It has to be int main(int argc, char *argv[]), not int main().
    SDL expects, nay, demands it take those two parameters.

  2. If your main is in a C++ file, chances are your ‘main’ function is not
    really called ‘main’ as far as the linker’s concerned, but
    1502347xcgxf532189_main@ or some other garbage. C++ mangles the names to
    prevent overloaded functions from having the same names.

Fortunately, there’s a way to tell C++ to do that. Try:
extern “C” int main(int argc, char *argv[])
instead of
int main()

this must be a VC++ thing then because linux doesnt require either of these
-mike


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Actually, it’s a gcc-mingw thing (for some reason), which happens to
occur quite frequently to the newbies (no offense intended).

Tim is right on the money about the formatting of the main() function,
make sure it looks identical to the way he’s shown.
Adding -Dmain=SDL_main to the compiler options (if my memory serves me
well) in Dev-C++ will do the trick, along with the options already
listed above.
Let us know if you get it working!

-Elden

Thank you very much. All I had to do was add the int argc, char *argv[]
parameters to main and it linked fine. I would have never thought that those
parameters would have been the problem.On 9/29/05, Tyler Montbriand wrote:

  1. It has to be int main(int argc, char *argv[]), not int main().
    SDL expects, nay, demands it take those two parameters.

  2. If your main is in a C++ file, chances are your ‘main’ function is not
    really called ‘main’ as far as the linker’s concerned, but
    1502347xcgxf532189_main@ or some other garbage. C++ mangles the names to
    prevent overloaded functions from having the same names.

Fortunately, there’s a way to tell C++ to do that. Try:
extern “C” int main(int argc, char *argv[])
instead of
int main()

and tell me how it goes :slight_smile:


Tim Swast
Purple {Programming} Monkey King

Glad to hear it wasn’t something horrible and life threatening :wink:
Best of luck with your project.

-Elden

Tim Swast wrote:> Thank you very much. All I had to do was add the int argc, char

*argv[] parameters to main and it linked fine. I would have never
thought that those parameters would have been the problem.

On 9/29/05, Tyler Montbriand <tsm at accesscomm.ca <mailto:tsm at accesscomm.ca>> wrote:

1) It has to be int main(int argc, char *argv[]), not int main().
SDL expects, nay, *demands* it take those two parameters.

2) If your main is in a C++ file, chances are your 'main' function
is not
really called 'main' as far as the linker's concerned, but
1502347xcgxf532189_main@ or some other garbage.  C++ mangles the
names to
prevent overloaded functions from having the same names.

Fortunately, there's a way to tell C++ to do that.  Try:
  extern "C" int main(int argc, char *argv[])
instead of
  int main()

and tell me how it goes :)


Tim Swast
Purple {Programming} Monkey King



SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.11.9/116 - Release Date: 9/30/2005