4

I'm trying to create a SDL2 project with Eclipse Kepler and MinGW on Windows. I already added SDL2 libs in MinGW (.a) in C:\MinGW\lib, SDL2 include in MinGW(C:\MinGW\include\SDL2) and I also added in projects properties -> C/C++ general -> paths and symbols -> librairies the following lines in that order :

mingw32
SDL2main
SDL2

Then I put '-mwindows' in MinGW C++ linker at the end of the line "Command line pattern"

I also added -Dmain=SDL_main for the entry point...

But the compiler gives me error :

main.cpp:7: undefined reference to `SDL_CreateWindow'

this is the code :

#include <SDL2/SDL.h>

int main(int, char**)
{
    SDL_Window *pWindow = nullptr;

    pFenetre = SDL_CreateWindow("Test SDL 2.0", 0, 0, 320, 240, SDL_WINDOW_SHOWN);
    if (!pWindow)
    {
        return -1;
    }

    SDL_DestroyWindow(pWindow);

    return 0;
}

And this is the build console :

Info: Internal Builder is used for build
g++ "-LC:\\MinGW\\lib" -o Test.exe main.o -lmingw32 -lSDL2main -lSDL2 -mwindows 
main.o: In function `SDL_main':
C:\Users\olivi_000\workspace\Test\Debug/../main.cpp:7: undefined reference to `SDL_CreateWindow'
C:\Users\olivi_000\workspace\Test\Debug/../main.cpp:13: undefined reference to `SDL_DestroyWindow'
C:\MinGW\lib/libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: erreur: ld a retourné 1 code d'état d'exécution

what's wrong ?

Oliveira
  • 43
  • 1
  • 1
  • 4
  • What's wrong is that the linker can't find `SDL_CreateWindow` function which likely means the options being passed to gcc from eclipse is wrong. Can give more detail if you show the actual build commands being invoked by eclipse. – greatwolf Jul 10 '13 at 19:41
  • Hi, i edited my post to show the actual build commands – Oliveira Jul 11 '13 at 15:59
  • You're building an executable so `-shared` shouldn't be in there during linking. – greatwolf Jul 11 '13 at 20:51
  • What version of mingw gcc are you using? I just tested your code snippet on mingw gcc 4.6.3 with sdl2 precompiled binaries from libsdl.org and it works fine. – greatwolf Jul 12 '13 at 02:41
  • Hi, i removed the '-shared' option and nothing changed, I still have the same errors plus another one : 'undefined reference to WinMain'. I will edit my post tonight with the new build command/errors. I m using this version of MinGW http://sourceforge.net/projects/mingw/ and the development libraries version of SDL 2 'SDL2-devel-2.0.0-mingw.tar.gz' available here : http://www.libsdl.org/tmp/download-2.0.php – Oliveira Jul 12 '13 at 07:23
  • Note, you can get the exact version with `gcc --version`. – greatwolf Jul 12 '13 at 09:07
  • Re, i edited my post and show the new build commands. And what the gcc --version said : 4.7.2 – Oliveira Jul 12 '13 at 16:15
  • There's also a `SDL2-devel-2.0.0-VC.zip`. Mingw can work with VC import libraries so see if sdl2.lib works. Another possibility is to link directly with `SDL2.dll`. eg. `g++ -mwindows -Wall -g -O0 -o Test.exe main.cpp -L C:\MinGW\lib -lsdl2main SDL2.dll` – greatwolf Jul 13 '13 at 01:09
  • Make sure you're using the *right* version of the library. You can't mix 64-bit import libraries with 32-bit compiler. `i686-w64-mingw32` is 32-bit; `x86_64-w64-mingw32` is for 64-bit. – greatwolf Jul 13 '13 at 01:15
  • Nice it works ! i used the wrong version of the SDL library, i used x86_64-w64-mingw32 instead of i686-w64-mingw32. I didn't know i686 means 32-bit version... Thank you very much ! How can i lock this post and make your answer the right answer ? – Oliveira Jul 13 '13 at 08:04

1 Answers1

4

Make sure you're using the right version of the library. You can't mix 64-bit import libraries with 32-bit compiler. For the SDL2 library you downloaded(SDL2-devel-2.0.0-mingw.tar.gz) it comes with both 32-bit and 64-bit. i686-w64-mingw32 is 32-bit and x86_64-w64-mingw32 is for 64-bit.

greatwolf
  • 20,287
  • 13
  • 71
  • 105