简体   繁体   English

无法链接Hello World!

[英]Can't link Hello World!

Guys that is code copied from a book (Programming Windows 5th edition): 从书中复制代码的人(编程Windows第5版):

#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. 无法用VS2010编译它。 What am I doing wrong? 我究竟做错了什么?

Error   1   error LNK2001: unresolved external symbol _WinMainCRTStartup

Thanks. 谢谢。

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. 在VS2010中,如果我通过File-> New-> Project,Visual C ++,Empty Project创建一个新项目,然后添加一个新的C ++文件,并复制你的代码,它编译并运行就好了。

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) 尝试在解决方案资源管理器中右键单击项目,转到属性 - >链接器 - >系统,并将子系统设置为“Windows(/ SUBSYSTEM:WINDOWS)

The Win32 APIs are old, but for the most part are perfectly usable if you want to do native Windows programming. Win32 API已经过时了,但如果您想进行本机Windows编程,则大部分都是完全可用的。 Windows has done a great deal of work to ensure that as long as you've followed the documentation, old APIs will not change. Windows做了大量工作,以确保只要您遵循文档,旧的API将不会更改。 You can still compile 16-bit Windows 3.1 code from 1992 and run it on 32-bit Windows 7. 您仍然可以从1992年编译16位Windows 3.1代码并在32位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" 编辑:也可能是在Properties-> C / C ++ - > Advanced中,您将Omit Default Library Name设置为“Yes”,您可能希望将其设置为“No”

Or also Properties->Linker->Input->Ignore Default Libs should be set to No. 或者还应将Properties-> Linker-> Input-> Ignore Default Libs设置为No.

You need to set the project's linker settings to create a Windows GUI program rather than a console program: 您需要设置项目的链接器设置以创建Windows GUI程序而不是控制台程序:

  • "Linker/System/SubSystem" should be set to "Windows (/SUBSYSTEM:WINDOWS)" “Linker / System / SubSystem”应设置为“Windows(/ SUBSYSTEM:WINDOWS)”

Usually this setting gets set properly when you create the project and choose a Windows GUI Application template. 通常,在创建项目并选择Windows GUI应用程序模板时,此设置会正确设置。 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). 不幸的是,如果项目设置为不正确的应用程序类型,设置有点埋没(我花了一些时间去挖掘它)。

Make sure you have actually added the source file to the project. 确保您已将源文件实际添加到项目中。 This worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM