C++ SDL2 - Incomprehensible link error

谁说胖子不能爱 提交于 2020-06-29 08:00:26

问题


I setup SDL2 under C++ CDT Eclipse. I added the include path "........SDL2-2.0.3\i686-w64-mingw32\include", the library path "........SDL2-2.0.3\i686-w64-mingw32\lib" and added the libraries 1."mingw32" 2."SDL2main" 3."SDL2". So now, if I add a main.cpp with this content:

#include <SDL.h>

int main(int argc, char* args[])
{

    return 0;
}

I can build the project fine, but if I use this:

#include <SDL.h>

int main()
{

    return 0;
}

The project can't build and I get this error:

Info: Internal Builder is used for build g++ "-IO:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\include\SDL2" -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\main.cpp" g++ "-LO:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\lib" -o "SDL OpenGL Lab.exe" main.o -lmingw32 -lSDL2main -lSDL2 O:\Eclipse CDT Workspace\SDL OpenGL Lab\Libraries\SDL2\lib/libSDL2main.a(SDL_windows_main.o): In function console_main': /Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x86/../src/main/windows/SDL_windows_main.c:140: undefined reference toSDL_main' collect2.exe: error: ld returned 1 exit status

I simply wonder me why this error is depending on my main method, I want to use the main method I want. Why this is so, how I can fix this ?


回答1:


Sorry, you're stuck with it. I also preferred a simpler main, but SDL doesn't support it.

Here's a little more on SDL and main in the Windows world. It doesn't say why you need their version of main -- but you do.

I get "Undefined reference to 'SDL_main'" ...

Make sure that you are declaring main() as:

#include "SDL.h"

int main(int argc, char *argv[])

https://wiki.libsdl.org/FAQWindows




回答2:


I had the same problem and had to go through a lot of manipulations. What I have found is the simple line #define SDL_MAIN_HANDLED before calling SDL.h. SDL has his own main that's why, I think. For example, in my .h files, I write everytime this :

#include <stdio.h>
#include <stdlib.h>
#define SDL_MAIN_HANDLED 
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>


来源:https://stackoverflow.com/questions/26218183/c-sdl2-incomprehensible-link-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!