2

I have been trying to use g++ to compile a program with SDL.

The program is only a main.cpp file with two SDL test lines in it, like this:

#include "SDL/SDL.h"

using namespace std;

int main(void) {
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Quit();

    return 0;
}

But my problem is that I get an error to do with the SDL libraries.

First of all I installed MinGW32 so that I could learn about C++11, so when I ask what version GCC is it says that it is version 4.7.0.

Then I downloaded the SDL-devel-1.2.15-mingw32.tar.gz file from the SDL website and extracted the folder to the desktop.

After that I copied all of the header files from /SDL-1.2.15/include/SDL to C:/MinGW/include/SDL

Then I copied 3 files from the /SDL-1.2.15/lib folder to the C:/MinGW/lib folder

They were:

libSDL.dll.a
libSDL.la
libSDLmain.a

And finally I copied the SDL.dll file from the bin directory to the same directory that the .exe file will be compiled from.

I think I have set everything up correctly, but I get two different messages based on how I try to compile it.

First of all I tried compiling it using the build system functionality in the Sublime Text 2 editor.

This is the command that I have told Sublime Text 2 to run:

"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}", "-lmingw32 -lSDLmain -lSDL -mwindows"]

When I run this I get the error message:

c:/mingw/bin/../lib/gcc/mingw32/4.7.0/../../../../mingw32/bin/ld.exe: cannot find lmingw32 -lSDLmain -lSDL -mwindows
collect2.exe: error: ld returned 1 exit status
[Finished in 0.3s with exit code 1]

So I then tried to compile directly from the command prompt.

I changed the directory to the same directory as my main.cpp and ran the following:

g++ main.cpp -o main.exe -lmingw32 -lSDLmain -lSDL -mwindows

And got the following error message:

c:/mingw/bin/../lib/gcc/mingw32/4.7.0/../../../libSDLmain.a(SDL_win32_main.o): In function `console_main':
/Users/slouken/release/SDL/SDL-1.2.15/./src/main/win32/SDL_win32_main.c:315: undefined reference to `_SDL_main'
collect2.exe: error: ld returned 1 exit status

Can anybody see what I am doing wrong?

Why is it that I get two different messages depending on where I run the command from, and how can I fix this?

Any help would be appreciated.

Lucas
  • 10,476
  • 7
  • 39
  • 40
  • Have you tried giving explicit paths for the libraries? If it's too much hassle to do on the command line, you can use a GUI like Code::Blocks. – Antimony Jul 08 '12 at 02:39
  • @Antimony Sorry for asking, but I'm quite new at this, but how do I give explicit paths for the libraries? Do I just use -lC:\path\here? And I was thinking about using CodeBlocks, but I like Sublime Text 2 much more so I wanted to try getting it to work in that first. If however I can't get it working for whatever reason I might be forced to use CodeBlocks for now. – Lucas Jul 08 '12 at 02:41
  • Sorry, I have no experience running build tools from Sublime. I always use Code::Blocks for C++ development. – Antimony Jul 08 '12 at 02:44
  • @Antimony Ok, thank you for taking the time to comment. I will carry on experimenting :) – Lucas Jul 08 '12 at 02:46

1 Answers1

5

I seem to have solved my own problem.

The error I was getting in the windows command prompt was because SDL appears to require the correct signature for the main() function so I changed:

int main(void) {

to:

int main(int argc, char *argv[]) {

This allowed me to compile from the command prompt without any problems.

Then the Sublime Text 2 error turned out to be a problem with the way I had written the build system file.

I just needed to split up the -l flags into separate values.

Previously I had the flags written like this:

"-lmingw32 -lSDLmain -lSDL -mwindows"

But I needed to change it to this:

"-lmingw32", "-lSDLmain", "-lSDL", "-mwindows"

And now I can build from Sublime Text 2 by hitting F7.

Lucas
  • 10,476
  • 7
  • 39
  • 40