Msbuild: ProjectReference to C++/CLI project does not work in MSBuild

Created on 13 Nov 2019  路  4Comments  路  Source: dotnet/msbuild

Steps to reproduce

I created C# console project (CSharp.csproj) that references C++/CLI project (CppLibrary.vcxproj). I can build it successfully in Visual Studio 2019 but it fails in MSBuild.

https://github.com/dermeister0/CppTest

CppLibrary.vcproj:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup Label="Globals">
    <VCProjectVersion>16.0</VCProjectVersion>
    <ProjectGuid>{1F9B2017-173D-491D-92A5-8C3D98295220}</ProjectGuid>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <Keyword>ManagedCProj</Keyword>
    <RootNamespace>CppLibrary</RootNamespace>
    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
  </PropertyGroup>

</Project>

CSharp.csproj:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{94E5E1C8-7CA0-4F31-A415-3EC85235A3C8}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>CSharp</RootNamespace>
    <AssemblyName>CSharp</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
  </PropertyGroup>

    <ProjectReference Include="..\CppLibrary\CppLibrary.vcxproj">
      <Project>{1f9b2017-173d-491d-92a5-8c3d98295220}</Project>
      <Name>CppLibrary</Name>
    </ProjectReference>

</Project>

Build.cmd:

call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools\VsDevCmd.bat"

git clean -xdf

msbuild CppLibrary\CppLibrary.vcxproj
msbuild CppTest.sln

Expected behavior

CppLibrary\Debug\CppLibrary.dll is referenced successfully.

Actual behavior

CoreCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\csc.exe /noconfig /nowarn
  :1701,1702 /nostdlib+ /platform:anycpu32bitpreferred /errorreport:prompt /warn:4 /define:DEBUG;TRACE /highentropyva+
  /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\Microsoft.CSharp.dll
  " /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\mscorlib.dll" /ref
  erence:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Core.dll" /refere
  nce:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Data.DataSetExtensio
  ns.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Data.
  dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.dll" /re
  ference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Net.Http.dll" /r
  eference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Xml.dll" /refer
  ence:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Xml.Linq.dll" /debu
  g+ /debug:full /filealign:512 /optimize- /out:obj\Debug\CSharp.exe /ruleset:"C:\Program Files (x86)\Microsoft Visual
  Studio\2019\Professional\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset" /subsystemversi
  on:6.00 /target:exe /utf8output /deterministic+ /langversion:7.3 Program.cs Properties\AssemblyInfo.cs "C:\Users\Azim
  in\AppData\Local\Temp\1\.NETFramework,Version=v4.7.2.AssemblyAttributes.cs"
  Using shared compilation with compiler from directory: C:\Program Files (x86)\Microsoft Visual Studio\2019\Profession
  al\MSBuild\Current\Bin\Roslyn
Program.cs(13,25): error CS0246: The type or namespace name 'CppLibrary' could not be found (are you missing a using di
rective or an assembly reference?) [C:\Work\CppTest\CSharp\CSharp.csproj]
Done Building Project "C:\Work\CppTest\CSharp\CSharp.csproj" (default targets) -- FAILED.

Done Building Project "C:\Work\CppTest\CppTest.sln" (default targets) -- FAILED.


Build FAILED.

"C:\Work\CppTest\CppTest.sln" (default target) (1) ->
"C:\Work\CppTest\CSharp\CSharp.csproj" (default target) (2) ->
(CoreCompile target) ->
  Program.cs(13,25): error CS0246: The type or namespace name 'CppLibrary' could not be found (are you missing a using
directive or an assembly reference?) [C:\Work\CppTest\CSharp\CSharp.csproj]

    0 Warning(s)
    1 Error(s)

Environment data

msbuild /version output:

Microsoft (R) Build Engine version 16.3.2+e481bbf88 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

16.3.2.50909

OS info: Windows 10 x64

If applicable, version of the tool that invokes MSBuild (Visual Studio, dotnet CLI, etc): VS 2019 16.3.7

Most helpful comment

@dermeister0
In your test solution file, its first configuration|platform is "Debug|Any CPU". So msbuild CppTest.sln uses "Any CPU" as platform. (In solution file, "Any CPU" setting misses CLR project build)

msbuild CppTest.sln /p:Platform=x86 will give you the success build. But you will see a warning.

The warning is caused by the inaccurate solution file configuration for projects.

@wli3
I believe this issue can be closed

All 4 comments

@wli3 can you take a look?

@dermeister0 Is this C++/CLI project targeting dotnet core? And do you mean projectreference instead of PackageReference ?

Is this C++/CLI project targeting dotnet core?

No, it's targeting .NET 4.7.2.

And do you mean projectreference instead of PackageReference ?

Sorry, wrong title. I updated it.

Test project: https://github.com/dermeister0/CppTest

@dermeister0
In your test solution file, its first configuration|platform is "Debug|Any CPU". So msbuild CppTest.sln uses "Any CPU" as platform. (In solution file, "Any CPU" setting misses CLR project build)

msbuild CppTest.sln /p:Platform=x86 will give you the success build. But you will see a warning.

The warning is caused by the inaccurate solution file configuration for projects.

@wli3
I believe this issue can be closed

Was this page helpful?
0 / 5 - 0 ratings