我有一个包含C ++和C#混合项目的项目,其中C ++做得很繁重,而C#提供了到外部的接口。 为了能够在C ++编译器之间切换,我们将解决方案移至CMake。 C ++部分可以工作,但是C#依赖于Google protobuffer,它起作用了。 这是我能够产生的最小示例:Cmake代码
cmake_minimum_required(VERSION 3.12)
add_library(
MyCSProject
SHARED
Test.cs
)
set_target_properties(SEM.Maeve PROPERTIES VS_PACKAGE_REFERENCES "Google.Protobuf_3.11.4;Grpc.Tools_2.25.0;MathNet.Numerics_4.8.0-beta01"
DOTNET_TARGET_FRAMEWORK_VERSION "v4.5"
)
add_dependencies(MyCSProjectMyProjectWrapperCpp) # MyProjectWrapperCpp is generated before
target_compile_options(MyCSProject PUBLIC "/unsafe" "/langversion:7")
其中Test.cs就是:
using System;
using Google.Protobuf.Collections;
namespace Test
{
public struct Test
{
public RepeatedField<int> data;
}
}
在VS中(使用最新的16.6.0版本,并使用VisualStudio 19作为生成器)生成的编译为
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Roslyn\csc.exe /noconfig /unsafe+ /nowarn:1701,1702 /nostdlib+ /platform:x64 /define:TRACE /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll" /debug+ /debug:full /optimize- /out:obj\x64\RelWithDebInfo\MyProject.dll /ruleset:"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Team Tools\Static Analysis Tools\\Rule Sets\MinimumRecommendedRules.ruleset" /subsystemversion:6.00 /target:library /utf8output /langversion:7.3 C:\sw_source\MyProject\src\MyProject\Test.cs "obj\x64\RelWithDebInfo\.NETFramework,Version=v4.5.AssemblyAttributes.cs"
没有Protobuf传递给编译器的痕迹,并且毫无疑问,代码返回错误:
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'Google' could not be found (are you missing a using directive or an assembly reference?) 2
Error CS0246 The type or namespace name 'RepeatedField<>' could not be found (are you missing a using directive or an assembly reference?) 8
要使它与protobuf链接,我缺少什么?如果我打开生成的解决方案,则编译正常,因为csporj文件包含了预期的内容:
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.11.4" />
<PackageReference Include="Grpc.Tools" Version="2.25.0" />
<PackageReference Include="MathNet.Numerics" Version="4.8.0-beta01" />
</ItemGroup>
尽管如此,我还是想让它在我进行开发的同一个VS实例中进行编译。 编辑:错误似乎是随机的:在某些重建中,程序包已正确链接,我将向Microsoft报告此错误