Unable to link to SDL2 functions using MinGW

36,943

Solution 1

A possible problem is that your linking to a SDL2 library for a different architecture.

You should be using

SDL2-2.0.1\i686-w64-mingw32

instead of

SDL2-2.0.1\x86_64-w64-mingw32

Also after setting the library search path use this notation to link to libraries

-lmingw32 -lSDL2main -lSDL2

It's much easier to read.

Solution 2

Instead of adding full path, try -LC:/PATH_TO_SDL -lSDL2main -lSDL2 (or only one SDL option) instead; the lib and .a is already known by the linker.

(not sure if you have to use either / or \)

Since you are using C++, replace your include with this:

extern "C"
{
    #include "SDL.h"
}

This tells the compiler to treat SDL as C - and not C++ - code.

See also: SDL2 won't link properly

Share:
36,943
Admin
Author by

Admin

Updated on May 05, 2020

Comments

  • Admin
    Admin about 4 years

    I'm relatively new to programming and I've decided to give SDL a try, but I'm a bit stuck. I haven't been able to build the project in codeblocks and I get 'undefined reference' to all SDL functions. I've seen a lot of similar questions here, but none of the solutions seems to help. I've already added the \include\SDL2 and the \lib folders to search directories, I've added SDL2Main and SDL2 to link libraries in linker options, I've even added -mwindows to other linker options. Also, I tried linking against the 64-bit version as well, but things got even worse.

    Here's my source code, pretty much copied straight out of the tutorial I started:

    #include <SDL.h>
    SDL_Window* g_pWindow = 0;
    SDL_Renderer* g_pRenderer = 0;
    int main(int argc, char* args[])
        {
            // initialize SDL
        if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
        {
        // if succeeded create our window
            g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL",
            SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
            640, 480,
            SDL_WINDOW_SHOWN);
            // if the window creation succeeded create our renderer
            if(g_pWindow != 0)
            {
                g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
            }
        }
        else
        {
            return 1; // sdl could not initialize
        }
        // everything succeeded lets draw the window
        // set to black // This function expects Red, Green, Blue and
        // Alpha as color values
        SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255);
        // clear the window to black
        SDL_RenderClear(g_pRenderer);
        // show the window
        SDL_RenderPresent(g_pRenderer);
        // set a delay before quitting
        SDL_Delay(5000);
        // clean up SDL
        SDL_Quit();
            return 0;
    }
    

    And here's the build log:

    mingw32-g++.exe -LC:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib  -o bin\Debug\GeometryProject.exe obj\Debug\main.o   -mwindows  C:\MinGW\lib\libmingw32.a C:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2main.a C:\dev\sdl\SDL2-2.0.1\x86_64-w64-mingw32\lib\libSDL2.a 
    obj\Debug\main.o: In function `SDL_main':
    C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:7: undefined reference to `SDL_Init'
    C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:13: undefined reference to `SDL_CreateWindow'
    C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:17: undefined reference to `SDL_CreateRenderer'
    C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:27: undefined reference to `SDL_SetRenderDrawColor'
    C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:29: undefined reference to `SDL_RenderClear'
    C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:31: undefined reference to `SDL_RenderPresent'
    C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:33: undefined reference to `SDL_Delay'
    C:/Users/Kris948/Desktop/ProjectsUni/GeometryProject/main.cpp:35: undefined reference to `SDL_Quit'
    C:\MinGW\lib\libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
    collect2.exe: error: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    9 errors, 0 warnings (0 minutes, 0 seconds)
    

    Is there anything else I could try? I really would like to get this running and would appreciate any help.

  • Xonar
    Xonar over 10 years
    Look at the linker error it is handled as C functions. (C++ functions have their names mangled so that the compiler can identify type information and MinGW will display the type information with an undefined reference if it's there)
  • Admin
    Admin over 8 years
    Thank you for the first hint. I thought GCC would emit useful answers regarding the incorrect architecture, but it didn't. I wasted hours on this nonsense.