0

Now I see that this is not a unique problem and has been raised plenty of times before, but I have followed the advice given in other stack overflow questions and nothing seems to help.

My problem is pretty straight forward, I can not compile my project (a basic c++ gui) and include SDL, because I get: Undefined reference to WinMain@16

I started using code::blocks but it didn't work so moved to trying to compile the simplest implementation on the command line in windows 7, 64 bit, in an attempt to understand whats going on in the background.

The command I am running:

g++ test.cpp -L C:\Projects\C++\tester\SDL\lib -lmingw32 -LSDLmain -LSDL -mwindows

I had a weird issue with the -l parameter in that I had added in a path to the SDL includes folder, C:\Projects\C++\tester\SDL\includes but if I do it chucks a error saying it cannot find this directory, obviously I checked and rechecked the path to make sure it was correct, but leaving it out removes the error. Most likely the cause of my problem now I thing about it.

The answer in this question is pretty comprehensive and helps understand the problem further but seems to suggest that the issue is that there is no main function defined, that seems to make sense, but doesn't SDL redefine main to SDL_main in SDL_main.h?

#define main SDL_main

I'd like to point out I am a c++ noob.

#include <string.h> 
#include "SDL\include\SDL.h" 
#include <iostream> 

int main(int argc, char*argv[]) 
{ 
    return 0; 
} 
Community
  • 1
  • 1
  • How is your `main` defined? Is it `int main()` or `int main(int argc, char *argv[])`? Try with the one with the `argc`/`argv` arguments and see if the problem persists! – Shahbaz Mar 12 '14 at 14:51
  • I've had this problem. You might be using 32 bit library when you need 64 bit, or vice versa. – BWG Mar 12 '14 at 14:56
  • @Shahbaz I have already tried a few vairent of the main function int main(int argc, char *argv[]), main(int argc, char **) but to no avail. :( – user2481985 Mar 12 '14 at 14:59
  • @BWG do you mean a 32 version of SDL? There are two folders in SDL/lib 84x and 64x I have tried to change the path to point to either without success – user2481985 Mar 12 '14 at 15:03
  • @user2481985 Yeah, that is what I mean. 32 bit with `int main(int argc, char*argv[])` should work. I wrestled with the same problem you are having for quite a while, and that was the fix. – BWG Mar 12 '14 at 15:14
  • @BWG Unfortunatley that has not fixed it.. Sad times indeed. This is what my .cpp looks like: #include #include "SDL\include\SDL.h" #include int main(int argc, char*argv[]) { return 0; } – user2481985 Mar 12 '14 at 15:26
  • @user2481985, instead of commenting, try to put the `.cpp` file in your question, along with your OS version and architecture so one may be able to reproduce it. – Shahbaz Mar 13 '14 at 10:29
  • @Shahbaz I had already mentioned the Windows Version in the original question (64 bit Windows 7), I have now included the code. I am unsure what you mean by architecture. Thanks – user2481985 Mar 13 '14 at 11:41
  • 64-bit was clear enough for the architecture ;) – Shahbaz Mar 13 '14 at 12:37

1 Answers1

1

Ok I spotted your error.

As you probably know, -L specifies link path while -l specifies the library to be linked. Your command says:

-L C:\Projects\C++\tester\SDL\lib -lmingw32 -LSDLmain -LSDL -mwindows
                                            ^^        ^^

Which mistakenly uses -L instead of -l. You meant to write:

-L C:\Projects\C++\tester\SDL\lib -lmingw32 -lSDLmain -lSDL -mwindows

Edit: regarding libSDL2.

First of all, you need to include <SDL2/SDL.h>. You also need to link against -lSDL2main and -lSDL2.

But the main problem here is that you are linking against -lmingw32, which expects a 32-bit architecture, while you are building and linking against the 64-bit version of libSDL2. If you build and link against the 32-bit version of libSDL2, all would be ok.

If your MinGW installation is 32-bits, then you are stuck with 32-bit builds and you need to use 32-bit libraries. If it is 64-bits, -lmingw32 needs to be replaced. Perhaps with -lmingw64, but I don't have a 64-bit MinGW installed to test it and I don't know if -lmingw64 actually exists or not.

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • Thanks for the reply. Couple of things that are confusing, firstly LSDMain and SDL are located at the path denoted at -L, well they are not really as they are called SDL2Main and SDL2. Whats going on there? listing the directories in -l only show the default ming32 libs. If I try and specify anything with -l it fails and claims it cannot find x. Even if its -l c:/, so I cant tell mingw to include a folder. – user2481985 Mar 13 '14 at 14:06
  • About including, I realized that your include path `"SDL\include\SDL.h"` should actually be `"SDL\SDL.h", and then give `-I C:\path\to\SDL\include`. Nevertheless, I tested with libsdl1.2. – Shahbaz Mar 13 '14 at 14:08
  • Since you are using libsdl2, and if you can see `SDL2Main` and `SDL2`, then go ahead and link with those two instead! So `-lSDL2main -lSDL2`. – Shahbaz Mar 13 '14 at 14:09
  • I have managed to get it to compile without error if I include a int WINAPI WinMain() function in my cpp, -L doesnt seem to care about SDL or SDL2 so that seems messed up and I have no idea how it it finds SDL. my include directory is located at SDL/includes/SDL changing this path give me an error saying it cannot find the header file. – user2481985 Mar 13 '14 at 14:18
  • Even though it now compiles, I can create SDL_Window *win = NULL, SDL_CreateWindow dies with undefined reference to SDL_CreateWindow. Not sure where to start now, should I fix it so I shouldnt need a WinMain function and why is -l failing to include any directory. Thanks for you help. – user2481985 Mar 13 '14 at 14:18
  • See my edit. The problem seems to be with a mismatch between your MinGW installation and SDL2 installation (in terms of 32 vs 64 bits). – Shahbaz Mar 13 '14 at 14:21