17

I am working on learning the windows API and am using mingw as my compiler with Code::Blocks as my IDE. I have run into an issue with using the wWinMain function. I used the program located here link text. It compiles fine on VSC++ 2008 express but when using mingw i get the "undefined reference to WinMain@16" error. I have figured out what the problem is (i think). By replacing the wWinMain with just Winmain and the String pointer PWSTR with LPSTR it compiles perfectly. My question is, how can i fix this, and if not, is not using Unicode that big of a deal.

Thanks.

contrapsych
  • 1,919
  • 4
  • 29
  • 44

3 Answers3

28

For old versions of MinGW, you can use a wrapper:

mingw-unicode-main:

https://github.com/coderforlife/mingw-unicode-main/

Simple wrappers to add wmain and wWinMain support in MinGW

These wrappers allow for use of wmain / wWinMain in MinGW seamlessly with Unicode (WCHAR), regular (CHAR), or the ability to choose (TCHAR).

The instructions for using them are in the files. Also take a look at other programs that use them.


For new versions of MinGW, you should use the -municode option, like it says in the mingw-unicode-main readme:

Note: This should no longer be used as MinGW now has a built-in solution. Add -municode to the command line (and possibly extern "C" to the wmain function).

The -municode option works with MinGW-w64. In 2012-07, when I tried MinGW, it did not have the -municode option.

Here is how to install MinGW-w64:

Target Win32:

Target Win64:


Unicode-related questions:

Community
  • 1
  • 1
XP1
  • 6,910
  • 8
  • 54
  • 61
  • 6
    Might be worthwhile to add that `-municode` is a ***linker* setting**. – Marc.2377 Oct 31 '16 at 05:29
  • 1
    @Marc.2377 you are right, the option belongs on the linking step. Many build systems do C++ builds using g++ compiler as the linker, since it is slightly easier than driving `ld` properly. When I tried this option myself with CMake (target_link_options) the error message came from `c++`. – cardiff space man Mar 07 '20 at 20:35
18

Use the plain (non unicode) WinMain function, and then get your unicode command line using GetCommandLineW. MinGW doesn't know about wWinMain.

You are probably going to find working on MinGW difficult; last time I used it it did not support most of the wchar_t components of the C++ standard library (i.e. std::wifstream, std::wstring, etc) which made interfacing with Unicode Windows bits difficult.

Do you have anything against MSVC?

Community
  • 1
  • 1
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • I dont have anything against MSVC, its just i thought one cant release software you make with the express edition. Even though im a long ways from that point, i want to be able to legally when i can. – contrapsych Aug 26 '10 at 00:51
  • 8
    @JAKE: There is no restriction on what kind of software you may build with MSVC Express Editions. The only difference between the full and express editions are that the full editions have *significantly* more features, such as the ability to install MSVC plugins (i.e. Visual Assist X). But there is no restriction on the compiled code -- it comes not from Visual Studio in any case but from the Windows SDK. – Billy ONeal Aug 26 '10 at 00:53
  • Oh, shows what all I know, thanks, I think I will probably start using VS then. – contrapsych Aug 26 '10 at 00:57
  • Now with the VS Community Edition (which replaces the Express Editions), there are even less differences to the commercial versions. In fact, the Community Edition is feature-equivalent to the Pro version (since VS 2015 I believe). The difference is only in how the license can be used. – zett42 Sep 13 '18 at 20:56
4

I know that I should have commented instead of answering, but I don't have enough reputation.

I want to add that I had to change the links to exe files in [Settings > Compiler... > Toolchain Executables > Program Files] in order to get the version of Community to run.

Also my CodeBlocks from 2016 claimed that it was Unicode but the -municode option didn't work, only the MiniGW update to the version by Community worked.

If you want to use main instead of wmain again, you have to delete -municode option.

jww
  • 97,681
  • 90
  • 411
  • 885
  • “The version **of** Community” or “the version **by** Community”? You say both things. Both are entirely different things. One means “the [Community](https://visualstudio.microsoft.com/vs/community/) version of Visual Studio recommended by @zett42”. The other means “the [MinGW-W64](https://mingw-w64.org/) version of MinGW (as opposed to [TDM-GCC](http://tdm-gcc.tdragon.net/)) recommended in the [answer](/revisions/11706847/3) edited by [@Community](/users/-1/community)”. Educated guess is impossible because both meanings make equal sense in the context of this thread. – 7vujy0f0hy Dec 21 '18 at 02:45
  • I thought the answer by "XP1" from 2012_07_29 was by "Community", but now I learned that "Community" is a bot and only edited it. Here I repeat the link from his post: https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/rubenvb/gcc-4.7-release/ I took version 4.7.2. – Hansebenger Dec 22 '18 at 15:27