Linker error

R

Red Hammer

Hi, I am new to using Visual C++ and trying to test it out. This is the
simple program I wrote to test it:

#define WIN32_LEAN_AND_MEAN

#include <windows.h> // the main windows headers
#include <windowsx.h> // a lot of cool macros

// main entry point for all windows programs
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
// call message box api with NULL for parent window handle
MessageBox(NULL, "THERE CAN BE ONLY ONE!!!",
"MY FIRST WINDOWS PROGRAM",
MB_OK | MB_ICONEXCLAMATION);

// exit program
return(0);

} // end WinMain

ok, it will compile no problem but when it comes to linking I get the
following error:

Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/demo2_2.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

I get this error when using either Visual C++ 5.0 or 6.0

the file LIBCD.lib is resident in the lib folder...

Any help much appreciated, Thanks
 
J

Jim Langston

----- Original Message -----
From: "Red Hammer" <[email protected]>
Newsgroups: alt.comp.lang.c++.help,alt.comp.lang.learn.c-c++,comp.lang.c++
Sent: Monday, July 04, 2005 8:20 PM
Subject: Linker error

Hi, I am new to using Visual C++ and trying to test it out. This is the
simple program I wrote to test it:

#define WIN32_LEAN_AND_MEAN

#include <windows.h> // the main windows headers
#include <windowsx.h> // a lot of cool macros

// main entry point for all windows programs
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
// call message box api with NULL for parent window handle
MessageBox(NULL, "THERE CAN BE ONLY ONE!!!",
"MY FIRST WINDOWS PROGRAM",
MB_OK | MB_ICONEXCLAMATION);

// exit program
return(0);

} // end WinMain

ok, it will compile no problem but when it comes to linking I get the
following error:

Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/demo2_2.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

I get this error when using either Visual C++ 5.0 or 6.0

the file LIBCD.lib is resident in the lib folder...

Any help much appreciated, Thanks

You are mixing window's code with non windows code. All programs must have
a: int main(whatever) function as the initial function to call.

You won't see that call a lot of times in windows programs because it's
hidden down in windows initialization, which would eventually call WinMain.
Since you aren't using those, just change

int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)

to

int main()

as for all the parameters that aren't being passed, you aren't using them
anyway.

You need to study windows programming if you want to do windows. Otherwise
you'll be writing console programs.
 
D

David White

Red said:
Hi, I am new to using Visual C++ and trying to test it out. This is
the simple program I wrote to test it:

#define WIN32_LEAN_AND_MEAN

#include <windows.h> // the main windows headers
#include <windowsx.h> // a lot of cool macros

[snip]

This does not pertain to the standard C++ language. Please ask in a Windows
or Visual C++ newsgroup. A number of suitable groups are listed here:
http://www.slack.net/~shiva/welcome.txt

DW
 
B

benben

Any help much appreciated, Thanks

What are the compiler options did you choose? Make sure you defined the
macro WIN32, either by #define (before #includes), or by the /DWIN32
compiler option.

ben
 
M

Me

int WINAPI WinMain(HINSTANCE hinstance,
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main

You're probably setting the linker flag /subsystem:console so either
remove it or set it to /subsystem:windows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top