6

I'm in need of parsing unicode parameters, so I wanted to use the wmain instead.

So instead of

int main(int argc, char** argv)

I would like to use

int wmain(int argc, wchar_t** argv)

The problem is that the visual studio is not recognizing the wmain, and it is trying to use main instead:

error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup

This is what I tried:

  • Changing the Properties->General->Character set
  • Changing the Entry point (In this case I got lot of compatibility errors with libraries that don't even have entry point, so it can't be specified there).

    warning LNK4258: directive '/ENTRY:mainCRTStartup' not compatible with switch '/ENTRY:mainWCRTStartup'; ignored
    
  • Tried the _tmain instead, just to find out it is just a macro that changes it to main.

  • Using the #pragma comment(linker, "/SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup")
  • Using the UNICODE macro

Nothing helps.

Edit: I would like to mention, that I'm using the vs120_xp (Win xp compatibile) toolset, but when I tried to use the default one, it still didn't work.

Edit2: I tried to make brand new project, and the wmain worked there out of the box. I didn't have to change anything, so it have to be some specific setting in the current project that is causing it.

kovarex
  • 1,766
  • 18
  • 34

2 Answers2

13
  Using the #pragma comment(linker, "/SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup")

You are getting close, not quite close enough. The CRT has four entrypoints:

  • mainCRTStartup => calls main(), the entrypoint for console mode apps
  • wmainCRTStartup => calls wmain(), as above but the Unicode version
  • WinMainCRTStartup => calls WinMain(), the entrypoint for native Windows apps
  • wWinMainCRTStartup => calls wWinMain(), as above but the Unicode version

So it is /ENTRY:wmainCRTStartup

Do beware that the command line arguments are converted to Unicode assuming the default console code page. Which is a bit unpredictable, it is the legacy 437 OEM code page only in Western Europe and the Americas. The user might need to use the CHCP command (CHange Code Page) and tinker with the console window font to keep you happy. YMMV.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I have tried that, but then I got this warning: Main.obj : warning LNK4258: directive '/ENTRY:mainCRTStartup' not compatible with switch '/ENTRY:wmainCRTStartup'; ignored – kovarex Feb 05 '15 at 13:32
  • You've got the option specified more than once and they disagree. Either use the #pragma OR use the Linker + Advanced + Entry Point setting, don't do both. – Hans Passant Feb 05 '15 at 13:35
  • I can't use the pragma, as it gives me exactly the same error (incompatibile switch), I don't use the settings anyhwere else, I checked the command line arguments for linking and the parameter is not there, but the pragma still didn't work. I also tried to make a new project, and the wmain worked without any special configuration, so it needs to be some magic bound to the current project. – kovarex Feb 05 '15 at 13:43
  • I can't guess at this if you don't show me the project file. Well, creating a new project is clearly a workaround. – Hans Passant Feb 05 '15 at 13:47
  • Ok, so the error was really unexpected one, more in my answer. – kovarex Feb 05 '15 at 15:35
1

I was merging the differences between new project that was working properly and our project for hours until I found out that the problem is caused by our misconfiguration of the allegro preprocesor definitions.

Deep down in the allegro library in win/alconfig.h, there are these lines

#ifndef ALLEGRO_NO_MAGIC_MAIN
  #if defined _MSC_VER && !defined ALLEGRO_LIB_BUILD
    #pragma comment(linker,"/ENTRY:mainCRTStartup")
  #endif
#endif

We did setup this macro for the allegro library compilation, but the allegro file was also included from the main project, that didn't specify this one. Defining the macro in the main project fixed the problem (obviously).

I didn't really see this comming!

kovarex
  • 1,766
  • 18
  • 34