undefined reference to WinMain@16 C++, SDL-2

馋奶兔 提交于 2019-11-30 15:53:01

问题


I've been getting the error undefined reference to WinMain@16. To save space, here's a link to all the files currently in the project. At present, it shouldn't do much other than create a window, fill it in green and then draw a box in the corner, all the while tracking my mouse's position through the console. However, it won't build and I'm given the aforementioned error.

My linker libraries are:

  • glew32s
  • libSDL2main
  • mingw32
  • libSDL2
  • opengl32
  • glew32

I am using Codeblocks 13.12 with g++ following the C++11 ISO C++ language standard. My PC is using windows 10 in case that's relevant.

I've spent quite a while trying to find a solution and it seems everyone has an entirely different one, and so far none of them have worked for me. A few examples include:

  • Adding SDL2_image to my link libraries in accordance with this similar question. It gave me the error -lSDL2_image.
  • Starting the project entirely from scratch in case some typo I failed to notice was the cause. (I got exactly the same error).
  • Changing my main function to WinMain. I got the same error message.
  • Adding a Windows.h header. This did nothing noticeable.
  • Various alleged "right orders" for my liker libraries. None of those have seemed to help thus far.

I should also point out that the answer provide by the user, Cheers and hth. - Alf, here might be what I'm looking for, but in all honesty I can't fully understand what I'm supposed to do in based on it.

If there's any relevant information that I forgot to include, please tell me and I'll do so as soon as possible. Thanks in advance.


回答1:


I think you want

#define SDL_MAIN_HANDLED

in your main file, BEFORE the line

#include <SDL2/SDL.h>

Explanation:

In SDL / SDL2, in an effort to try to make cross-platform development of certain kinds of applications simpler, SDL creates a custom "entry-point" to your application. That is, your int main() is not the real main. What happens is, main is defined as a macro in the SDL header and this causes your main to be renamed to SDL_main or similar. Then, in the "SDL_main" library a different main is defined which will be the real main of your application. This main just fetches the command-line arguments in whatever way is appropriate for the platform, and calls your main (which was renamed SDL_main).

On windows, there are also some differences regarding whether your application is started as a console program vs. as a gui program, iiuc.

Sometimes you want SDL to do these things for you, but if you are developing a traditional console program, usually you don't. So you pass SDL this SDL_MAIN_HANDLED define in order to prevent from doing all this stuff.

The #undef main approach will work also, but it's not quite as good because this way, you tell SDL what is going on, with the other method, SDL thinks that all it's stuff is going to be used and in fact you crudely disable it with #undef later.

If you want to see details of the various macros / platform checks, you can look in the SDL_main.h header. If you want to know what the benefits of the SDL main system are, you can look at SDL documentation.



来源:https://stackoverflow.com/questions/32342285/undefined-reference-to-winmain16-c-sdl-2

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