11
votes

I've recently decided to try working with SDL with CodeBlocks 10.05. I started with the tutorial on http://www.sdltutorials.com/sdl-tutorial-basics and did my best to follow it. Unfortunately, I'm encountering:

..\..\..\..\..\..\SDL\SDL-1.2.15\lib\libSDLmain.a(SDL_win32_main.o):SDL_win32_main.c|| undefined reference to `SDL_main'|

when I try to compile it.

I've searched through many of the questions on this website and other tutorials (mainly the tutorial on LazyFoo and the CodeBlocks wiki) and can't seem to find a solution.

  • C:\SDL\SDL-1.2.15\include has been added in the Compiler tab (Search Directories)
  • C:\SDL\SDL-1.2.15\lib has been added in the Linker tab
  • The libraries libmingw32.a, libSDLmain.a, libSDL.dll.a are linked in that order
    • libmingw32.a from the MinGW\lib folder in the CodeBlocks installation directory
  • SDL.dll is in both the System32 folder and in the project folder

When attempting to follow the tutorial on the CodeBlocks wiki, I was told that SDL.h could not be found in the given directory (when making a new SDL project).

CApp.cpp

#include "CApp.h"
#include "SDL\SDL.h"

CApp::CApp(){
    Surf_Display=NULL;

    Running=true;
}

int CApp::OnExecute(){
    if (OnInit()==false){
        return -1;
}

SDL_Event Event;

while (Running){
    while (SDL_PollEvent(&Event)){
        OnEvent(&Event);
    }
    OnLoop();
    OnRender();
}

OnCleanup();
return 0;
}

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

    return theApp.OnExecute();
}

CApp.h

#ifndef CAPP_H_INCLUDED
#define CAPP_H_INCLUDED
#include "SDL\SDL.h"

class CApp{
    private:
        bool Running;
        SDL_Surface* Surf_Display;

    public:
        CApp();
        int OnExecute();

    public:
        bool OnInit();
        void OnEvent(SDL_Event* Event);
        void OnLoop();
        void OnRender();
        void OnCleanup();
};



#endif // CAPP_H_INCLUDED
5
IIRC you need to supply SDL_main, instead of having a main you have an SDL_mainMusa
@Musa I'm sorry, could you possibly explain what you meant by that? I'm relatively new to programming and don't quite understand. Does this have to do with my setup of SDL (Linking and whatnot) or is it a problem within the code? Thanks!Prismriver
Never mind, I see this is done in SDL_main.hMusa
There's got to be something missing in your linker setup. The way you described it, it should work (and it indeed does, for me at least).jrok
@jrok I just tried redoing all of the linking following what the tutorial stated as well as the points listed above. To walkthrough what I did: - Downloaded SDL-1.2.15-win32-x64.zip (64-bit Windows) and SDL-devel-1.2.15-mingw32.tar.gz (Mingw32) from the SDL website. - Extracted the first one into C:\SDL and placed the SDL.dll from the second into the project folder and system32 - Added the \include and \lib in the compiler and linker tabs respectively - Linked libmingw32.a, libSDLmain.a, and libSDL.dll.a in that order.Prismriver

5 Answers

32
votes

put these arguments to the main function. I had this problem too, and I fixed it few seconds ago.

int main(int argv, char** args) { }

18
votes

Try #undef main after all SDL related headers.

Update. This is not a valid solution!

As pointed out by HolyBlackCat, this is a pretty sloppy fix. SDL replaces the main function in order to perform some initialization and/or cleanup that is otherwise not possible, and then calls back to the actual user function.

The interception works by replacing the name of user's main function to SDL_main, with a simple macro

#define main SDL_main

The user's function then ceases to be the entry point for the application, and an entry point provided by SDL is used. The proposed #undef disables the interception recklessly and one should argue that it is not supposed to work at all. For those who successfully compiled and ran an SDL application after this "fix", it must have simply been a platform-dependent coincidence.

The proper solution to the OP's error is making sure that the file containing main gets compiled and linked, and that the function has correct signature. As already posted by others.

7
votes

The only plausible reason for your problem I can think of is that when you created the file with main in it, you forgot to add it to build targets.

enter image description here

You should see CApp.cpp in the list where my main.cpp is. Right click on it and click Properties. Click on Build tab in the window that pops up. You should see this:

enter image description here

Click OK, hit Ctrl+F11 (Rebuild).

Good luck.

1
votes

Recently, I had this problem, watched some YouTube videos and also followed the installation guide by LazyFoo and I kept getting the "Undefined reference to SDL_main" error.

I followed everything said here after successfully linking the minGw files to my project properties, but it didn't work until I added to my main function int main (int argv, char** args) and voila, it worked.

using namespace std;

int main(int argv, char** args) {    if(SDL_Init(SDL_INIT_VIDEO)<0)    {
       cout<<"Endl Init Failed."<<endl;
       return 1;    }

   cout<<"SDL Init succeeded."<<endl;

   SDL_Quit();    return 0; }

Why it worked is still not clear to me but it worked anyway.

0
votes

Define SDL_MAIN_HANDLED before you include the SDL libs:

#define SDL_MAIN_HANDLED
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>

More info: I'm using the SDL functions without the SDL_main be defined. Is that fine?