Author Topic: undefined reference to `WinMain@16'  (Read 32886 times)

voidclass

  • Guest
undefined reference to `WinMain@16'
« on: June 20, 2006, 08:07:55 am »
Source:
#include <iostream>

int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
{
   std::cout << "Hello world!" << std::endl;
   return 0;
}

Build log:
Compiling: main.cpp
Linking console executable: bin\Debug\echod.exe
C:\MinGW\lib/libmingw32.a(main.o):main.c:(.text+0x106): undefined reference to `WinMain@16'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 10 seconds)
0 errors, 0 warnings

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: undefined reference to `WinMain@16'
« Reply #1 on: June 20, 2006, 08:55:29 am »
MinGW presently does not support wmain (don't have any information on whether it might in the future).
I am not sure if wmain is canon, anyway. It smells very much like a home-brewn Microsoft thing (but I may of course be wrong). Nevertheless, I've seen it around a couple of times, and it is well-known for making trouble.

The "correct" way to solve this problem as you can find on the internet is to use the CRT function __wgetmainargs from inside a normal main function. That function is neither documented, nor is it declared in the headers, so you'll have to properly declare it yourself before using it.
Personally, I have never bothered to do that (I perceive this as a hack, and I'd rather just use main), so I can't tell you an exact recipe for that.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."


Offline Vampyre_Dark

  • Regular
  • ***
  • Posts: 255
  • Hello!
    • Somewhere Over The Rainbow...
Re: undefined reference to `WinMain@16'
« Reply #3 on: June 20, 2006, 11:07:18 am »
wchar is a built in type?

If you just want a hello world, you don't need all that, a simple

int main(BLANK)
{
...
      return 0;
}

will do if you won't be passing the program arguments.
C::B Wishlist
~BOYCOTT THE EVIL YELLOW BOXES~

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: undefined reference to `WinMain@16'
« Reply #4 on: June 20, 2006, 11:20:19 am »
wchar is a built in type?
No, but wchar_t is.

Quote from: Michael
--> especially: http://www.hardforum.com/showpost.php?p=1028709866&postcount=48
I still perceive this as a hack, as it uses Windows API to do something that should just work in an OS-agnostic way.
But at least, CommandLineToArgvW is documented and prototyped. So I guess you've really found the best way to do it there :)

That is... if one actually needs the commandline. Vampyre Dark's objection was not entirely wrong, if you don't use the commandline parameters anyway, then of course plain old int main() is sufficient.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: undefined reference to `WinMain@16'
« Reply #5 on: June 20, 2006, 11:35:30 am »
I still perceive this as a hack, as it uses Windows API to do something that should just work in an OS-agnostic way.

Hello,

But wmain is Microsoft specific AFAIK:

Quote
[...]
Visual C++ Language Reference
Using wmain Instead of main

Microsoft Specific

In the Unicode programming model, you can define a wide-character version of the main function. Use wmain instead of main if you want to write portable code that adheres to the Unicode specification.

You declare formal parameters to wmain using a similar format to main. You can then pass wide-character arguments and, optionally, a wide-character environment pointer to the program. The argv and envp parameters to wmain are of type wchar_t*.

If your program uses a main function, the multibyte-character environment is created by the operating system at program startup. A wide-character copy of the environment is created only when needed (for example, by a call to the _wgetenv or _wputenv functions). On the first call to _wputenv, or on the first call to _wgetenv if an MBCS environment already exists, a corresponding wide-character string environment is created and is then pointed to by the _wenviron global variable, which is a wide-character version of the _environ global variable. At this point, two copies of the environment (MBCS and Unicode) exist simultaneously and are maintained by the operating system throughout the life of the program.

Similarly, if your program uses a wmain function, an MBCS (ASCII) environment is created on the first call to _putenv or getenv, and is pointed to by the _environ global variable.

For more information on the MBCS environment, see Single-byte and Multibyte Character Sets in the Run-Time Library Reference.
END Microsoft Specific
[...]

Best wishes,
Michael

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: undefined reference to `WinMain@16'
« Reply #6 on: June 20, 2006, 11:38:36 am »
Ah, so my first suspicion was right... I knew I smelled a fish  8)

Anyway, CommandLineToArgvW looks like the best thing. :)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

voidclass

  • Guest
Re: undefined reference to `WinMain@16'
« Reply #7 on: June 20, 2006, 01:46:52 pm »
thank everyone.