Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linker error in Microsoft C++ 1

Status
Not open for further replies.

EM

Programmer
Oct 21, 2000
5
0
0
US
Does anyone know why I'm getting this error when linking this program. It compiles OK, just fails when linking:

error LNK2001: unresolved external symbol _WinMain@16

Here's the program I'm working on:

//A simple game of guess the number

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cstdlib>

#include <iomanip>

#include <ctime>


int main()
{
int num, guess=0;
char play='y';
srand (time(0));

while (play='y'){
num= rand() % 1000;
cout<< num;

cout<<&quot;I have a number between 1 and 1000.\n&quot;;
cout<<&quot;Can you guess my number? \n&quot;;
cout<<&quot;Please type your first guess.\n&quot;;
cin>>guess;

while (guess != num){

if( guess > num){
cout<<&quot;Too high. Try again.&quot;;
}
else {
cout<<&quot;Too low. Try again.&quot;;
}
cin>> guess;
}

cout<<&quot;Excellent! You guessed the number!\n&quot;;
cout<<&quot;Would you like to play again(y or n)\n&quot;;
cin>> play;
}

return 0;

}

[sig][/sig]
 
Hi EM

for what it's worth,..

Does your compiler support win16 if so have you compiled as a win 16 then changed to a win 32 target?, BC++ 5 would do this if you changed target and recompiled this is an issue of the cache containing the previous targets references, to fix change the code a bit and recompile or delete the pre-compilied object files.

HTH [sig]<p>Robert Dwyer<br><a href=mailto:rdwyer@orion-online.com.au>rdwyer@orion-online.com.au</a><br>[/sig]
 
Robertd,
Thanks for replying.
My compiler only supports WIN 32 so I am unable to change to a WIN 16. Do you have any other suggestions as to how I can fix this error?

EM [sig][/sig]
 
Hi EM,

Like i don't know why i didn't notice before it's been a while since i fired up c++ zzzz) whats your target??
this looks like a console type app so you don't have a winmain() change the target to console its looking for a reference to winmain()

HTH [sig]<p>Robert Dwyer<br><a href=mailto:rdwyer@orion-online.com.au>rdwyer@orion-online.com.au</a><br>[/sig]
 
Robertd,

Guess what! I got it to work. I just trashed the old workspace and created a new one and it worked fine. Thanks for your help though! I really appreciate it. :)
EM [sig][/sig]
 
Hi,
Is very easy to fix. You need to create a Win32Console Applicatio but no a Win32 application. If you can't change, create an empty Win32 console application and add the source files. Also you can creae a Win32 Application, but instead main you must use WinMain
 
EM -

Like Robertd and JohnFill said, when you create a new project in MSC++, you need to choose what type of project to create. If you choose &quot;Win32 Application&quot;, then that's a true Windows app, and there's a ton of overhead you must type in before you can even do something as simple as display a message (buy the Charles Petzold book to get a start on this). For your project, you need to select &quot;Win32 Console Application&quot;, where you can do traditional C development.

If you want to save some time in the link step, hit Alt-F7 in your project and go to the &quot;link&quot; tab. You can remove a lot of the libraries at the end of the list (from shell32.lib to odbccp32.lib) since you won't be calling them.

You can see what Win32/c/c++ functions require which libraries by opening the WIN32API.CSV file (on my machine it's in: C:\Program Files\Microsoft Visual Studio\VC98\Lib). Use Excel or 1-2-3 to open it, and it'll make viewing and searching it easier.

Chip H.
 
error LNK2001: unresolved external symbol _WinMain.....
is a tipical project's error. It mean you created a WinAPI project. The functions cout, cin are not supported in WinAPI programs. The function main can be supported only if you change the options about the primary thread (_CRTStartup) what call the function WinMain from your program.
By the way, if you want to use MFC in console programs, you must change also in Project->Settings->General->Using MFC(as a shared DLL or as a static link library).
The program you made is a tipical console application and the function WinMain have nothing to do there. You can remove, change, add any library without any effects. The type of project is wrong.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top