MSBuidel - 将资源文件添加到项目后静态库编译失败

msbuild - static lib compilation failed after adding resource file to project

本文关键字:静态 编译 失败 项目 资源 源文件 添加 MSBuidel      更新时间:2023-10-16

我有一个C++库,它被编译为动态和静态库。最近我将资源版本文件添加到源。动态库编译工作正常,但静态库编译开始失败,64 位目标出现以下错误:

LINK : warning LNK4068: /MACHINE not specified; defaulting to X86
 x64Releasedllmain.obj : fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'

以下是我的编译脚本:

@ECHO OFF
call "%VS140COMNTOOLS%"\vsvars32.bat
SET SourceDir=D:ProjectsMySampleLib
SET TargetDir=D:ProjectsPackages
ECHO 32 bit MySampleLib .LIB compilation VS2010
msbuild.exe %SourceDir%MySampleLibMySampleLib.vcxproj /t:Clean;Rebuild /p:Configuration=Release;Platform=Win32;ConfigurationType=StaticLibrary;PlatformToolset=v100

ECHO 64 bit MySampleLib .LIB compilation VS2010
msbuild.exe %SourceDir%MySampleLibMySampleLib.vcxproj /t:Clean;Rebuild /p:Configuration=Release;Platform=x64;ConfigurationType=StaticLibrary;PlatformToolset=Windows7.1SDK

当 Lib.exe 命令尝试链接 MySampleLib.res 时发生此错误

注意:该错误仅在我添加资源文件后出现。我不想将资源文件添加到静态库。

我终于通过修改以下.vcxproj条目解决了这个问题

<ItemGroup>
    <ResourceCompile Include="MySampleLib.rc" />
</ItemGroup>

<ItemGroup Condition="'$(ConfigurationType)'!='StaticLibrary'">
    <ResourceCompile Include="MySampleLib.rc" />
</ItemGroup>

这阻止了在静态编译中链接资源文件。