Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...