oft Corporation. All rights reserved.
hello.c
D: est>link hello.obj
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.
LINK : fatal error LNK1561: entry point must be defined
此时,则出现LNK1561错误。MSDN上对应的解释为:
entry point must be defined
The linker did not find an entry point. You may have intended to link as a DLL, in which case you should link with the /DLL option. You may have also forgotten to specify the name of the entry point; link with the /ENTRY option.
Otherwise, you should include a main, wmain, WinMain, or wMain function in your code.
If you using LIB and intend to build a .dll, one reason for this error is that you supplied a .def file. If so, remove the .def file from the build.
因为/Zl去掉了defaultlib,所以link直接就是hello.obj,而不会去链接libc.lib,因为默认定义的entry symbol为mainCRTStartup,此时没有libc.lib,里面的mainCRTStartup也就没有,所以会提示出没有定义entry point错误。
Way1:
照MSDN上面的解释,我们再来将main函数使用上,然后做编译/Zl,以及链接工作会怎样呢?
首先将myentry改为main。之后:
D: est>cl /c /Zl hello.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
hello.c
D: est>link hello.obj
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
hello.obj : error LNK2001: unresolved external symbol _printf
LINK : error LNK2001: unresolved external symbol _mainCRTStartup
hello.exe : fatal error LNK1120: 2 unresolved externals
此时的错误又不相同,这里出现寻找不到printf和mainCRTStartup的链接错误。
Way2:
将/entry选项指定为myentry。
D: est>cl /c /Zl hello.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
hello.c
D: est>link /entry:myentry hello.obj
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
LINK : fatal error LNK1221: a subsystem cant be inferred and must be defined
又出现一个新错误,LNK1221。来看看解释:
a subsystem can’t be inferred and must be defined
The linker does not have enough information to infer which subsystem you will target your application.
To fix this error, use the /SUBSYSTEM option.
看来link的时候,还可以根据搜到的symbol以及定义的entry来自动判断编译出来的目标应用的subsystem,关于subsystem,似乎只有windows下面有这个编译选项,加上来再看一下。
D: est>link /entry:myentry /subsystem:console hello.obj
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
hello.obj : error LNK2001: unresolved external symbol _printf
hello.exe : fatal error LNK1120: 1 unresolved externals
OK,当加上之后,也出现了LNK2001错误,比上面要少一个_mainCRTStartup,还需要一个_printf。
Way3:
而综合上面两种方法,将way1和way2合起来使用,使用main函数代替掉myentry的源码,然后将main设置entry point。<