Link to home
Start Free TrialLog in
Avatar of Booth882
Booth882

asked on

glut/VC++4.0 difficulty

whenever I try to use glut with openGL in my VC++4.0 compiler, the program compiles but on the linking stage I get the error

libcd.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16

can anybody tell me whats going on?
Avatar of Booth882
Booth882

ASKER

Edited text of question
as in how I can go about resolving the unresolved external symbol _WinMain@16
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
WinMain() must be declared like

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
);

You can search for "WinMain" in the on-line docs to learn more about the parameters.
good, but what do I put as hInstance and lpCmdLine?  I have no clue
What do mean?  You don't call WinMain() so you don't have to supply those parameters.  Your WinMain() procedure will be called by the OS with these parameters "filled in".  Just like the "regular" main() procedure used to be called with the argc and argv parameters "filled in".

You are trying to create a windows GUI program aren't you?  If so you will have a WinMain() instead of a main().  It will take the parameters I listed above.  Look at the docs on winmain by searching for "WinMain" in the on-line help.

If you aren't trying to make a windows GUI program, you should have just a main() not a winmain().  But in that case you need a different project type.  You need a project to create a win32 console application.
ok heres the deal.  I have a program with a main loop, like

void main()
{
    code....
}

but when I link it I get the error above.  So as I understand you I need a WinMain() instead of a main(), which would make it

int WINAPI WinMain(hInstance, prevInstance, lpCmdLine, nCmdShow)
{
    code....
}

if that is the case, what do I put in as arguments?  I mean, what do I pass in as hInstance and prevInstance and lpCmdLine and nCmdShow?  

if that is not the case, please help me to understand what exactly it is that I need to do to get my program to link!
first of all:

In a typical C++ program you have

int main(int argc, char * argc)
{
 code
};

Note that main returns an int, it is not void.  You can ussualy get away declaring main() as returning void, but someday you won't....

Note that main() takes two arguments.  However, you are allowed to declare main without them, like

int main()
{
  code
};

Now that is for a standard C++ program.  Microsoft and then Borland and others decided to break the rules a little.  For a windows program you don't need (actually you can't have) a main() procedure.  Instead you have a procedure called WinMain() that serves the same purpose.  But WinMain() has two differences.  First, it is named differently--that is important because this name is required and main() is not allowed.  Second, it takes a different set of parameters (Ones that you may not alter.).

Now you ask?
>> what do I pass in as hInstance and prevInstance and lpCmdLine and nCmdShow?

Well, what do you pass when you call main()?  Huh???  You don't call main().  Main is called when the program starts.  You never call it.  Same thing with WinMain().  WinMain() is called when the program starts, you don't call it.  You just put code inside WinMain() that you would have placed inside main().  Again WinMain() replaces main().
Note that all this is true only if you are trying to create a GUI program (a "real" windows program).  If you want to create a win32 console program, then you would have an ordinary main().   A win32 console program looks like an "old fashioned" program.  That is, it displays just text, it doesn't have menus and buttons etc.

 I can't tell which you want.  But given the error you are getting, VC is trying to make a GUI program, thus, you need a WinMain().  If you want a consoel program, you cn keep your main(), but need to switch to a console project, not a GUI project.  

Thus if you still can't get it to compile, I need to know which type of program you are creating.
so show me how I would write it then.  is it just

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE previnstance, LPSTR lpcmdline, int ncmdshow)
{
    code....
}

then?
Exactly.
but to use glut I must call

   glutInit(int * argc, char ** argv)

which with main like

  int main(int argc, char ** argv)

I can simply go

   glutInit(& argc, argv);

my question is:  What are the WinMain equivalents to argc and argv?  are there even equivalents?  can I call such a function as glutInit using a WinMain program?
WinMain() gets a single parameter string, unlike main()'s nicely parsed parameter array.  Thus if you call a windows program called PROG with 3 parameters, you will get a string like
"PROG PARAM1 PARAM2 PARAM3"

You could convert this string into the data that glutinit needs. You would parse this information into an argv-like array and calculate an argc value.  (using strtok() it would be pretty easy)

However, it might not be worth it.  Since it is rare for a windows program to be called with parameters, you probably could just pass a 0 argc to glutinit indicating there are no prameters.  (I assume glutinit doesn't have to have parameters)  

I should mentuon that I am not familar with glut.  But considering the way it takes parameters, it suggests that it was not intended to be called from a true windows program.  Do you know if if can be?  What does it do?
perfect!  its all working.  thanks for your help once again
Just out of curiousity--What did you do?  pass a 0 argc or did you create an argvc-like array.