tags:

views:

850

answers:

5

Hey guys, this is my first post.

I'm just getting started with win32 API programming in C++ and I'm having trouble compiling the winnie tutorial (http://www.relisoft.com/win32/winnie.html) with MinGW.

My input and output:

C:\Users\Eric\Projects> g++ winnie.cpp -o winnie.exe

/mingw/lib/libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to 'WinMain@16'

collect2: ld returned 1 exit status

Any help would be very much appreciated.

My source code: http://pastebin.com/tfDTUcBp

+2  A: 

The line:

/mingw/lib/libmingw32.a(main.o):main.c:(.text+0xd2):
    undefined reference to 'WinMain@16'

means that the linker has a reference within MinGW's main.o which requires a WinMain function to be provided by the user code. The most likely cause of this is that you haven't provided one.

When you create Win32 applications, main is no longer the entry point you use. The entry point for your user code is now WinMain so you need to provide that rather than main:

int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nShowCmd);

The parameters are:

  • hInstance, a handle to your applications instance, where an instance can be considered to be a single run of your application.
  • hPrevInstance is always NULL.
  • lpCmdLine, a pointer to a string that holding any command-line arguments.
  • nShowCMD, which determines how your application's window will initially be displayed.

Now, having looked over your code and seen that you do actually have a WinMain, it's most likely an environment isssue rather than a code issue.

Have you tried compiling with -mwindows as an option?

A good start is to begin with the canonical beginner program and sort out the error there first. Enter the following program:

#include <windows.h>
int WinMain (HINSTANCE p1, HINSTANCE p2, LPSTR p3, int p4) {
    MessageBox (0, "Hello.", "MyProg", MB_OK);
    return 0;
}

and try to compile it the same way as your current program:

g++ -o qq.exe qq.cpp

Then try with:

g++ -mwindows -o qq.exe qq.cpp

and see if that works.

If it works in the former case, there's something wrong with your code. If not, there's probably something wrong with your compiler flags or environment.

And then, if adding -mwindows works, it should solve your problem with your real code as well. And, if not, keep working with the simpler program since that has less variables that can cause problems.

paxdiablo
I'm not using main() though. I'll post my source code.
Eric Larsen
-1, It's clear that the questioner is using WinMain else he wouldn't receive this error `/mingw/lib/libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to 'WinMain@16'`.
The Elite Gentleman
Source has been posted.
Eric Larsen
With respect, @Elite, you're wrong. "Undefined reference" means something is referencing it but it's not visible to the linker. Hence my thought that it wasn't defined. The reference is coming from libmingw, *not* the users code.
paxdiablo
@paxdiablo, I get what you're saying, but in this case, the Linker cannot link the WinMain function to a library. `main.c:(.text+0xd2)` shows that the WinMain has been created (or "addressed") in the data space in the `main.o` object hence I've been telling you that WinMain exists in main.c.
The Elite Gentleman
No, there is a _reference_ to WinMain in MinGW's main.c (not the user's main.c) which is unsatisfied by the user's code.
paxdiablo
Removed negative point since you included the correct switch.
The Elite Gentleman
that is because he is using *int WINAPI WinMain*, not *int WinMain*
br1
+1  A: 

Ugh, sorry. It turns out it was a typo. My bad.

Eric Larsen
And the typo was ... ? In addition, this should be left as a comment to your question since it's not *actually* an answer - I'm surprised it got an upvote, I have to assume someone missed :-)
paxdiablo
+1  A: 

Ok, Here's the switch that works for MinGW

-mwindows

Example

gcc hello.c -o hello.exe -mwindows

the -m switch sets the EUMULATION, so -mwindows sets the EMULATION to Windows.

It's quite different to Borland since Borland uses the -t switch.

e.g. bcc32 -tW (for Windows).

Hope this helps.

The Elite Gentleman
A: 

I've noticed that this exact same error occurs (mingw / Win XP / gcc in my case) if your source file doesn't have a main() function, even if you are doing nothing Windows related at all.

Wayne Koorts
A: 

It is the source code problem. It redefines the WinMain as:

int WINAPI WinMain (
    HINSTANCE hInst, 
    HINSTANCE hPrevInst, 
    char const * cmdParam, 
    int cmdShow)

The third parameter type is "char const *". it is different with required parameter type LPSTR. That's problem!

After change the parameter type back to LPSTR. It can be compiled as:

g++ winnie.cpp -o winnie.exe 

Even without a -mwindows option!

RouMao