Question

As I tried to upgrade from my graphics programming from "legacy openGL" x "SDL 1.x" to OpenGL3+ x SDL2 and I came across linking problems. I tried many linking parameters but nothing seems to work.

I tried SDL1 linking params: linking listing 1

-lmingw32 -lSDLmain -lSDL

It works fine with this code: code listing 1

#include <SDL2/SDL.h>

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

    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        SDL_Quit();

        return -1;
    }

    SDL_Quit();

    return 0;
}

but when I add SDL2 codes to it like this: code listing 2

#include <SDL2/SDL.h>

int main(int argc, char **argv)
{   
    SDL_Window* window(0);

    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        SDL_Quit();

        return -1;
    }

    window = SDL_CreateWindow("Test SDL 2.0", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

I get linking errors: error listing 1

g++     -o dist/Debug/MinGW-Windows/sdl2 build/Debug/MinGW-Windows/main.o  -lmingw32 -lSDLmain -lSDL
build/Debug/MinGW-Windows/main.o: In function `SDL_main':
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:21: undefined reference to `SDL_CreateWindow'
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:23: undefined reference to `SDL_DestroyWindow'

collect2.exe: error: ld returned 1 exit status

swaping linking params to this: linking listing 2

-lmingw32 -lSDL -lSDLmain

gives even more errors (I won't write them down). Adding a '2' to the params like this: linking listing 3

-lmingw32 -lSDL2main -lSDL2

gives me more linking errors than without the 2s: error listing 2

g++     -o dist/Debug/MinGW-Windows/sdl2 build/Debug/MinGW-Windows/main.o  -lmingw32 -lSDL2main -lSDL2
build/Debug/MinGW-Windows/main.o: In function `SDL_main':
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:14: undefined reference to `SDL_Init'
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:16: undefined reference to `SDL_Quit'
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:21: undefined reference to `SDL_CreateWindow'
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:23: undefined reference to `SDL_DestroyWindow'
C:\Users\Paikuhan\Documents\NetBeansProjects\SDL2/main.cpp:24: undefined reference to `SDL_Quit'
c:/programs/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/libmingw32.a(main.o): In function `main':
e:\p\giaw\src\pkg\mingwrt-4.0.3-1-mingw32-src\bld/../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/main.c:91: undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

as if no single SDL functions were found and I do have the libraries (libSDL2.a, libSDL2.dll.a, libSDL2.la, libSDL2main.a, pkgconfig\sdl2.pc) in the lib directory. I also tried using this set of params in netbeans: linking listing 4

$(pkg-config --cflags --libs sdl2)

and it doesn't do any good. Can anyone please help me on this. Thank you!

Was it helpful?

Solution

Problem Solved! I tried to troubleshoot the linking errors. Now everything works fine!! LazyFoo's tutorial has the answer "Most importantly i686-w64-mingw32 which contains the 32bit library [...] This is important: most compilers still compile 32bit binaries by default to maximize compatibility. [...]". So I re-extracted all the 32bit files in their corresponding directories. Now everything works like a charm. My guess is I used the 64bit libs with the 32bit mingw compiler.

both linking params work fine. this:

$(pkg-config --cflags --libs sdl2)

or this:

-lmingw32 -lSDL2main -lSDL2 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top