【问题标题】:LNK2001 error when compiling on command line, but not in IDE在命令行上编译时出现 LNK2001 错误,但在 IDE 中没有
【发布时间】:2013-07-11 09:35:58
【问题描述】:

我有一个在 Visual Studio 2010 中开发的大型 VC++ 项目,我需要能够使用 CL.exe 从命令行构建它。在IDE中,项目构建成功,但是我一直无法在命令行上构建。

我主要处理链接器错误并将其归结为一个:

LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup
C:\<path_to>\Debug\LargeProject.exe : fatal error LNK1120: 1 unresolved externals

我在调试文件夹中找到了一个名为“LargeProject.log”的文件,该文件显示了在 IDE 构建时从 IDE 调用的精确命令行参数。如果有帮助,我已在此处列出它们。

(有关一些更新,请参阅此问题底部的编辑说明)

CL.exe /c /Zi /nologo /W3 /WX- /Od /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D _MBCS /D _AFXDLL /Gm- /EHa /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yc"StdAfx.h" /Fp"Debug\LargeProject.pch" /Fo"Debug\\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt "Source Files\stdafx.cpp" /wd4482 > testDump.txt
CL.exe /c /Zi /nologo /W3 /WX- /Od /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D _MBCS /D _AFXDLL /Gm- /EHa /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yu"StdAfx.h" /Fp"Debug\LargeProject.pch" /Fo"Debug\\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt <list of all source files except the following> /wd4482 > testDump.txt
CL.exe /c /Zi /nologo /W3 /WX- /Od /Oy- /D WIN32 /D _WINDOWS /D _DEBUG /D _MBCS /D _AFXDLL /Gm- /EHa /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yu"StdAfx.h" /Fp"Debug\LargeProject.pch" /Fo"Debug\\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt "Source Files\LargeProject.cpp" /wd4482 > testDump.txt
rc.exe /D _DEBUG /D _AFXDLL /l"0x0409" /I"Debug\\" /nologo /fo"Debug\LargeProject.res" LargeProject.rc > testDump.txt
rc.exe /nologo /fo"Debug\LargeProject.exe.embed.manifest.res" "Debug\LargeProject_manifest.rc" > testDump.txt
link.exe /ERRORREPORT:PROMPT /OUT:"C:\<path_to>\Debug\LargeProject.exe" /INCREMENTAL /NOLOGO <list of libraries used> /MANIFEST /ManifestFile:"Debug\LargeProject.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\<path_to>\Debug\LargeProject.pdb" /SUBSYSTEM:WINDOWS /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\<path_to>\Debug\LargeProject.lib" /MACHINE:X86 "Debug\LargeProject.res" > testDump.txt
// note that I don't run any of the commands below because the above command
//   threw an error.
mt.exe /nologo /verbose /out:"Debug\LargeProject.exe.embed.manifest" /manifest "Debug\LargeProject.exe.intermediate.manifest" "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Include\Manifest\dpiaware.manifest" > testDump.txt
rc.exe /nologo /fo"Debug\LargeProject.exe.embed.manifest.res" "Debug\LargeProject_manifest.rc" > testDump.txt
link.exe /ERRORREPORT:PROMPT /OUT:"C:\<path_to>\Debug\LargeProject.exe" /INCREMENTAL /NOLOGO <list of libraries used> /MANIFEST /ManifestFile:"Debug\LargeProject.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\<path to>\Debug\LargeProject.pdb" /SUBSYSTEM:WINDOWS /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\<path_to>\Debug\LargeProject.lib" /MACHINE:X86 "Debug\LargeProject.res" > testDump.txt

如您所见,我按顺序运行每一个(直到出现错误)并将内容转储到 testDump.txt。这是第一个 link.exe 命令后 testDump.txt 的内容

LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup
C:\<path_to>\Debug\LargeProject.exe : fatal error LNK1120: 1 unresolved externals

我很困惑,我在命令行上收到此错误,但不是来自 IDE,它可能只是在幕后运行这些命令。还值得注意的是,我已经从命令提示符窗口运行了 vcvars32.bat 文件,所以它不可能是本地环境变量问题。

编辑: 我尝试将msvcrt.lib 添加到链接命令中的库列表中。这产生了一个新的链接器错误:

msvcrt.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
C:\<path_to>\Debug\LargeProject.exe : fatal error LNK1120: 1 unresolved externals

我读到这意味着项目中没有定义正确的入口点。即,项目中应该有一个 WinMain() 函数作为应用程序的入口点。这似乎合乎逻辑,但我还没有找到该功能,并且重申一下,该项目在 IDE 中编译和链接良好,并且大概我从中获得这些命令的日志文件只是列出了 IDE 正在执行的操作。

似乎还有其他 CRT 库。我尝试了msvcprt.lib,但在我添加任何内容之前,它产生了相同的错误消息。存在的其他类似 .lib 文件是 msvcmrt.libmsvcmrtdmsvcprtdmsvcrtdmsvcurtmsvcurtd。这些库文件之间有什么区别,有人如何选择使用哪一个?

【问题讨论】:

  • 为什么要链接你的应用程序两次?也许您忘记重新链接 CRT?
  • @JochenKalmbach 我尝试链接 CRT,请参阅我的问题中的新信息。 IDE 生成的 .log 文件两次列出了该链接命令。我从来没有这样做过,因为链接器错误是第一次发生。
  • 为什么要直接用“cl”构建呢?为什么不使用 devenv 在命令行上构建您的项目????同样在项目设置中,您将在高级选项中看到 cl/link 命令行。
  • @JochenKalmbach 我需要通过代码分析工具运行项目,该工具需要具有有效的构建和链接命令才能运行。我注意到高级选项中的命令。它们与我一直使用的几乎相同,并且产生了相同的结果。我也尝试在 VS 命令提示符下执行此操作,结果相同。我真的很想知道 IDE 在做什么,而我没有。

标签: visual-studio-2010 visual-c++ linker cl.exe


【解决方案1】:

我遇到了同样的错误,我知道如何解决它。

在您的项目中,您选择了一个非 GUI 项目:这是一个错误并修复它选择(我不知道确切的名称,我在法语中使用 VS)

option->Link options->System->Sub-système=Windows 

它应该可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 2015-06-19
    • 2013-11-15
    相关资源
    最近更新 更多