tags:

views:

173

answers:

2

Guys that is code copied from a book (Programming Windows 5th edition):

#include <windows.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0) ;

     return 0 ;
}

Link to the topic in which this book is recommended. Can't compile it with VS2010. What am I doing wrong?

Error   1   error LNK2001: unresolved external symbol _WinMainCRTStartup

Thanks.

+5  A: 

It will depend on how you set up the project. In VS2010, if I create a new project via File->New->Project, Visual C++, Empty Project, then add a new C++ file, and copy your code in, it compiles and runs just fine.

If you've created a different type of project, it may be using different link libraries. Try right-clicking on your project in Solution Explorer, going to Properties->Linker->System, and setting SubSystem to "Windows (/SUBSYSTEM:WINDOWS)

The Win32 APIs are old, but for the most part are perfectly usable if you want to do native Windows programming. Windows has done a great deal of work to ensure that as long as you've followed the documentation, old APIs will not change. You can still compile 16-bit Windows 3.1 code from 1992 and run it on 32-bit Windows 7.

Edit: It could also be that in Properties->C/C++->Advanced, you have Omit Default Library Name set to "Yes", you probably want it set to "No"

Or also Properties->Linker->Input->Ignore Default Libs should be set to No.

Eclipse
I had set it as you suggested and still no go.
There is nothing we can do
@Knowing: Can you describe the process you're taking to create a new project?
Eclipse
@Know: Did you try creating a new project like Eclipse suggested?
Bill
@Eclipse: +1 for answering this question while named Eclipse (for those without a sense of humor, yes, I do actually find this answer useful)
Lord Torgamus
I'm creating project by selecting new->project and choosing Win32 project from templates. Then in next step I'm selecting empty project. After that I'm adding new c++ file and pasting this code.
There is nothing we can do
Ok so maybe from different side: Could anyone recommend good recent book on this subject?
There is nothing we can do
Petzold's book is the one that I'd recommend for learning Win32, so that's not of much help. What you could do, since it's a pretty small project, just post the contents of your .vcxproj file, then we can see what setting are on.
Eclipse
@Eclipse Your last line in your post did the trick. But it's somewhat frustrating that those settings are not set correctly with this type of project and one has to do it on his own. Sometimes I really wonder...
There is nothing we can do
+2  A: 

You need to set the project's linker settings to create a Windows GUI program rather than a console program:

  • "Linker/System/SubSystem" should be set to "Windows (/SUBSYSTEM:WINDOWS)"

Usually this setting gets set properly when you create the project and choose a Windows GUI Application template. Unfortunately, if the project is set to the incorrect application type, the setting is a bit buried (it took me bit of digging to find it).

Michael Burr