Undefined link to 'SDL_main'

Recently, I decided to try working with SDL with CodeBlocks 10.05. I started with a tutorial at http://www.sdltutorials.com/sdl-tutorial-basics and did my best to follow it. Unfortunately, I come across:

..\..\..\..\..\..\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 this.

I looked through many questions on this site and other tutorials (mainly a LazyFoo tutorial and CodeBlocks wiki) and it seems I can’t find a solution.

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

When I tried to follow the CodeBlocks wiki guide, I was told that SDL.h could not be found in this directory (when creating 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 
+6
source share
4 answers

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

enter image description here

You should see CApp.cpp in the list where my main.cpp is located. Right-click on it and select "Properties." Click on the "Construction" tab in the window that appears. You should see this:

enter image description here

Click OK, press Ctrl + F11 (Rebuild).

Good luck.

+5
source

Try #undef main after all the SDL related headers.

Refresh. This is an invalid solution!

As indicated by HolyBlackCat, this is a pretty messy fix. The SDL replaces the main function in order to perform some initialization and / or cleaning, which would otherwise be impossible, and then refers to the actual user-defined function.

Interception works by replacing the name of the main user function with SDL_main using a simple macro

 #define main SDL_main 

The user-defined function then ceases to be the entry point for the application, and the entry point provided by the SDL is used. The proposed #undef disables the interception recklessly, and it should be argued that it should not work at all. For those who successfully compiled and launched the SDL application after this β€œfix,” it should just be a platform-dependent coincidence.

The correct solution to the OP error is to ensure that the file containing main is compiled and linked, and that the function has the correct signature. As others have already written.

+18
source

put these arguments in the main function. I also had this problem, and I fixed it a few seconds ago.

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

+12
source

Define SDL_MAIN_HANDLED before enabling SDL_MAIN_HANDLED SDL:

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

Additional info: I use SDL functions without defining SDL_main. This is normal?

0
source

Source: https://habr.com/ru/post/1414945/


All Articles