使用 msbuild 通过命令行编译时出现 LNK2019 错误

LNK2019 errors while compiling through command line using msbuild

我第一次尝试使用以下命令通过命令行编译 VC++ 项目之一:

msbuild MyProject.vcproj

但我得到了很多

error LNK2019: unresolved external symbol…

所有这些符号都存在于一个库 *.lib 中,该库已编译并存在于指定路径中。如果我通过 VS IDE 构建,我的项目会成功构建,没有任何 LNK2019 错误。但是不知何故在命令行编译期间它无法 link 库。

如何解决此问题并使其在命令行编译期间使用 linked 库?


命令行选项:

/Od /I "../" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_USRDLL" /D "MYPROJECT_EXPORTS" /D "_WIN32_DCOM" /D "_CRT_SECURE_NO_WARNINGS" /D "_CRT_NON_CONFORMING_SWPRINTFS" /D "_BIND_TO_CURRENT_VCLIBS_VERSION=1" /D "_VC80_UPGRADE=0x0700" /D "_WINDLL" /D "_MBCS" /Gm /EHsc /RTC1 /MDd /Fo"..\output\debug\x64\" /Fd"..\output\debug\x64\vc90.pdb" /W3 /nologo /c /Wp64 /Zi /TP /errorReport:prompt

其他选项:

\Y-

我还注意到构建日志中显示的一件事:

Build started: Project: MyProject, Configuration: Debug|Win32 (TaskId:1)
Build started: Project: MyProject, Configuration: Debug|x64 (TaskId:1)
Compiling... (TaskId:1)
cl : Command line warning D9035: option 'Wp64' has been deprecated and will be removed in a future release
Build started: Project: MyProject, Configuration: Release|Win32 (TaskId:1)
  Compiling... (TaskId:1)
cl : Command line warning D9035: option 'Wp64' has been deprecated and will be removed in a future release
Build started: Project: MyProject, Configuration: Release|x64 (TaskId:1)

我很困惑为什么它甚至尝试构建任何 Win32 配置,因为我想为 x64 构建并使用相应的 Visual Studio 2008 x64 Win64 Command Prompt。虽然不确定这是否会导致我的问题。

您可能启动了 'visual studio command prompt for x64'(不确定确切名称),但 msbuild 使用的默认 configuration/platform 是 Debug/Win32。很难从提供的输出中判断接下来到底发生了什么,但它可能导致链接器在 64 位库中寻找 32 位符号,反之亦然 - 在任何情况下,混合平台。

要解决此问题,请具体说明您要构建的内容,例如:

msbuild MyProject.vcproj /p:Configuration=Debug;Platform=x64