0

I've been trying to compile a SDL2 code in C++, a console application. I use Code::Blocks with GNU GCC Complier. This is the code:

#include <iostream>

#include <SDL2/SDL.h>

int main( int argc, char *argv[] )
{
    if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0)
    {
        std::cout << "SDL could not initialise! SDL Error: " << SDL_GetError( )<< std::endl;
    }

    return EXIT_SUCCESS;
}

It gives me these errors:

undefined reference to `SDL_Init'
undefined reference to `SDL_GetError'
undefined reference to `WinMain@16'

I have "-lmingw32 -lSDL2main -lSDL2" in the other linker options, and and in Search directories I put:

  • ......\SDL2-devel-2.0.5-mingw\SDL2-2.0.5\x86_64-w64-mingw32\include in Compiler
  • ......\SDL2-devel-2.0.5-mingw\SDL2-2.0.5\x86_64-w64-mingw32\lib in Linker

I moved all include, lib and bin files to the MinGW's folders.

Any idea of what's what I'm doing badly?

Thank you!

11
  • 4
    How exactly does your linker command line look like? Remember: Order of librbaries and objects matters when linking. Commented May 27, 2017 at 16:13
  • Try writing #undef main on the line before int main(...) Commented May 27, 2017 at 16:14
  • 3
    @LaquinLaquih That is a path, not a command line. Commented May 27, 2017 at 16:19
  • 2
    @underscore_d I think SDL2 redefines main in windows, if my memory serves me right. I know that I had some problems without undefining main Commented May 27, 2017 at 16:20
  • 2
    Undefining main is a bad idea unless you absolutely know what you're doing (and in that case you wouldn't be asking questions), and is addressed by SDL FAQ. What is a full compile command (I suppose your IDE should show it during the build) and what is its full output? It is quite likely that you're using 32bit compiler but trying to link 64bit libraries, which just fails to find symbols on windows (instead of reporting wrong library architecture, like on other systems). Commented May 27, 2017 at 16:45

1 Answer 1

-4

For , the header file in the include directive must be:

#include <SDL.h>

<SDL2/SDL.h> is used for systems.

Sign up to request clarification or add additional context in comments.

1 Comment

It all depends on how you configure your build system, no? In any case, it couldn't cause undefined reference error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.