C++/CLI库从.NET Framework升级.NET Standard 2.0的log4net报错如何解决?
VS2019 C++/CLI项目升级.NET Standard 2.0 log4net引用报错修复方案
问题背景
当前为VS2019环境下的C++/CLI应用项目,目标从.NET Framework升级适配到.NET Standard 2.0。此前适配.NET Core 5.0时编译链接均正常,处理log4net引用环节出现问题:未将log4net接口传入类库时运行无异常,传入接口后开始报错。移除原有旧版log4net引用,替换为.NET Standard 2.0版本的引用后出现如下错误:
warning MSB3268: The primary reference "log4net" could not be resolved because it has an indirect dependency on the framework assembly "netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "log4net" or retarget your application to a framework version which contains "netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51". AFile.h(3): error C2871: 'log4net': a namespace with this name does not exist
原项目文件(.vcxproj)完整代码
<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|Win32"> <Configuration>Debug</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> <ProjectConfiguration Include="Release|Win32"> <Configuration>Release</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> </ItemGroup> <PropertyGroup Label="Globals"> <ProjectGuid>{9FAC29B5-62B2-4323-9469-921B467C8433}</ProjectGuid> <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion> <RootNamespace>theCppCliLibraryName</RootNamespace> <Keyword>ManagedCProj</Keyword> <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <ConfigurationType>DynamicLibrary</ConfigurationType> <PlatformToolset>v140_xp</PlatformToolset> <CharacterSet>Unicode</CharacterSet> <CLRSupport>true</CLRSupport> <WholeProgramOptimization>true</WholeProgramOptimization> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <ConfigurationType>DynamicLibrary</ConfigurationType> <PlatformToolset>v140_xp</PlatformToolset> <CharacterSet>Unicode</CharacterSet> <CLRSupport>true</CLRSupport> </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <ImportGroup Label="ExtensionSettings"> </ImportGroup> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> <PropertyGroup Label="UserMacros" /> <PropertyGroup> <_ProjectFileVersion>14.0.25431.1</_ProjectFileVersion> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <OutDir>$(SolutionDir)$(Configuration)\</OutDir> <IntDir>$(Configuration)\</IntDir> <LinkIncremental>true</LinkIncremental> <LinkKeyFile>theCppCliLibraryName.snk</LinkKeyFile> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <OutDir>$(SolutionDir)$(Configuration)\</OutDir> <IntDir>$(Configuration)\</IntDir> <LinkIncremental>false</LinkIncremental> <LinkKeyFile>theCppCliLibraryName.snk</LinkKeyFile> </PropertyGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ClCompile> <Optimization>Disabled</Optimization> <AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <PreprocessorDefinitions>WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> <PrecompiledHeader>Use</PrecompiledHeader> <WarningLevel>Level3</WarningLevel> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> </ClCompile> <Link> <AdditionalDependencies>LicenseLib2015MDd.lib</AdditionalDependencies> <AdditionalLibraryDirectories>../../lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <GenerateDebugInformation>true</GenerateDebugInformation> <AssemblyDebug>true</AssemblyDebug> <RandomizedBaseAddress>false</RandomizedBaseAddress> <DataExecutionPrevention /> <TargetMachine>MachineX86</TargetMachine> <Profile>true</Profile> <SubSystem>Windows</SubSystem> </Link> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ClCompile> <AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <PreprocessorDefinitions>WIN32;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions> <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> <PrecompiledHeader>Use</PrecompiledHeader> <WarningLevel>Level3</WarningLevel> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> </ClCompile> <Link> <AdditionalDependencies>LicenseLib2015MD.lib</AdditionalDependencies> <AdditionalLibraryDirectories>../../lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <GenerateDebugInformation>false</GenerateDebugInformation> <RandomizedBaseAddress>false</RandomizedBaseAddress> <DataExecutionPrevention /> <TargetMachine>MachineX86</TargetMachine> <SubSystem>Windows</SubSystem> <ShowProgress>LinkVerbose</ShowProgress> </Link> </ItemDefinitionGroup> <ItemGroup> <Reference Include="log4net"> <HintPath>..\3rdPartyLibraries\log4net-2.0.8\bin\net\4.5\release\log4net.dll</HintPath> </Reference> <Reference Include="System"> <CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies> <ReferenceOutputAssembly>true</ReferenceOutputAssembly> </Reference> <Reference Include="System.Data"> <CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies> <ReferenceOutputAssembly>true</ReferenceOutputAssembly> </Reference> <Reference Include="System.Drawing"> <CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies> <ReferenceOutputAssembly>true</ReferenceOutputAssembly> </Reference> <Reference Include="System.Windows.Forms"> <CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies> <ReferenceOutputAssembly>true</ReferenceOutputAssembly> </Reference> <Reference Include="System.Xml"> <CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies> <ReferenceOutputAssembly>true</ReferenceOutputAssembly> </Reference> </ItemGroup> <ItemGroup> <ClCompile Include="AFile.cpp" /> <ClCompile Include="BFile.cpp" /> <ClCompile Include="Stdafx.cpp"> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader> </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="AFile.h" /> <ClInclude Include="BFile.h"> <FileType>CppForm</FileType> </ClInclude> <ClInclude Include="resource.h" /> <ClInclude Include="Stdafx.h" /> </ItemGroup> <ItemGroup> <ResourceCompile Include="app.rc" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup> <ProjectExtensions> <VisualStudio> <UserProperties Spices_ObfuscationOptions="{Anonymizer=High}{AntiILDASM=CompleteImproved}{Members=Full}" /> </VisualStudio> </ProjectExtensions> </Project>
修复步骤
- 第一步:确认项目目标框架版本
错误提示当前项目实际生效的目标框架为.NET Framework 4.0,而.NET Standard 2.0最低要求.NET Framework 4.6.1以上版本。你项目文件中虽然标记了TargetFrameworkVersion为v4.6.2,但存在配置优先级问题,需手动确认:右键项目->属性->通用属性->目标框架,确认设置为.NET Framework 4.6.2或更高版本,同时清理所有隐式引用的低版本.NET Framework组件。 - 第二步:添加netstandard程序集引用
.NET Framework 4.6.2默认没有直接包含netstandard的引用,需手动在项目的引用节点中添加netstandard.dll引用,路径一般为C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\Facades\netstandard.dll,也可以直接在项目文件的ItemGroup引用节点下新增如下配置:<Reference Include="netstandard"> <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\Facades\netstandard.dll</HintPath> </Reference> - 第三步:修正log4net引用路径
你当前引用的是log4net的.net 4.5版本,并非.NET Standard 2.0版本,需替换为log4net 2.0.15及以上版本中netstandard2.0目录下的对应dll,修改项目文件中log4net的HintPath为正确路径即可。 - 第四步:升级平台工具集(可选)
你当前使用的是v140_xp工具集,若不需要兼容XP系统,可升级为v142(VS2019对应工具集),避免旧工具集对高版本.NET的兼容问题。 - 第五步:清理重建项目
删除项目的Debug、Release文件夹以及.vs缓存文件夹,重启VS后重新生成项目即可。
内容的提问来源于stack exchange,提问作者Sam Carleton
相关产品推荐
相关产品推荐

