在x64上运行C#C#C#C+/CLI包装器解决方案的问题

发布于 2025-01-17 17:42:31 字数 4385 浏览 1 评论 0 原文

我正在使用Visual Studio 2019版本16.11.10。我正在尝试做一个可以与功能与C ++对象通信的小型C#项目。我知道我必须使用称为C ++/CLI包装器的东西。

我试图遵循本教程:

该项目由一个带有3个项目的解决方案组成。这三个项目是Sandbox(C#项目),包装器(C ++/CLI包装器项目)和Core(C ++项目)。

有关核心项目的详细信息是,您必须将其配置为静态库(.lib)。为此,您将右键单击核心项目,然后选择属性。在一般情况下,我们选择静态库(.lib)。

有关包装器项目的一个详细信息是,您已经选择了“不使用预编译标头”,请属于properties-> c/c ++ - >

我已经完成了某种x86/x64的“对齐”项目中的x86/x64“对齐”。配置管理器中的项目。如果我选择x86作为解决方案平台,我将把沙箱设置为X86,将核心设置为Win32,并将包装器设置为Win32。如果我选择X64作为解决方案平台,我将将沙箱,核心和包装器设置为X64,

我遇到了在x86上运行解决方案的问题。问题在于包装器和沙盒使用了不同的.NET反面。因此,现在,如果两者都使用相同的.NET-VERSION(例如.NET 3.1或.NET 5),则用于包装器和沙盒,那么我可以在X86上运行解决方案。

我在X64上运行解决方案时遇到了问题。当我运行时,我会收到此错误:

Microsoft Visual Studio

调试可执行文件 'c:\ users \ philip \ kodning2 \ vsworkdir \ egnatestprojekt \ solution_cliexample3 \ core \ sandbox \ bin \ bin \ x64 \ debug \ debug \ net5.0 \ sandbox.exe'' 在“沙盒”调试配置文件中指定的不存在。

确定

我还会遇到这些错误:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)    Core    C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Core\MSVCRTD.lib(exe_main.obj) 1   
Error   C1010   unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?   Wrapper C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Wrapper\Entity.cpp 16  
Error   CS0246  The type or namespace name 'Entity' could not be found (are you missing a using directive or an assembly reference?)    Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 9   Active
Error   CS0246  The type or namespace name 'Entity' could not be found (are you missing a using directive or an assembly reference?)    Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 9   Active
Error   CS0246  The type or namespace name 'CLI' could not be found (are you missing a using directive or an assembly reference?)   Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 2   Active
Error   CS0006  Metadata file 'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Wrapper.dll' could not be found   Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\CSC    1   Active
Error   LNK1120 1 unresolved externals  Core    C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Core.exe 1   

如何能够以x64的形式运行该解决方案?我做错了什么?

Edit1:

正如Alan Birtles所建议的那样,我从输出窗口中发布文本:

Build started...
1>------ Build started: Project: Core, Configuration: Debug x64 ------
Restored C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Sandbox.csproj (in 3 ms).
1>Entity.cpp
1>MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
1>C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Core.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Core.vcxproj" -- FAILED.
2>------ Build started: Project: Wrapper, Configuration: Debug x64 ------
2>pch.cpp
2>AssemblyInfo.cpp
2>Entity.cpp
2>C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Wrapper\Entity.cpp(16,1): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?
2>Wrapper.cpp
2>Generating Code...
2>Done building project "Wrapper.vcxproj" -- FAILED.
3>------ Build started: Project: Sandbox, Configuration: Debug x64 ------
3>CSC : error CS0006: Metadata file 'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Wrapper.dll' could not be found
3>Done building project "Sandbox.csproj" -- FAILED.
========== Build: 0 succeeded, 3 failed, 0 up-to-date, 0 skipped ==========

I am using Visual Studio 2019 version 16.11.10. I am trying to do a small C# project that can communicate with an C++ object with functions. What i understand was that i have to use something called C++/CLI wrapper.

I tried to follow this tutorial: redgate - Creating a C++/CLI Wrapper

The project consists of one solution with 3 projects. The three projects are Sandbox (C# project), Wrapper (C++/CLI wrapper project) and Core (C++ project).

A detail about the Core-project is that you have to configure it as Static library (.lib). To do it you will Right-click on the Core-project and select Properties. Under General we select Static library (.lib).

A detail about the Wrapper-project is that you have select “Not Using Precompiled Headers” at Properties->C/C++->Precompiled Headers

I have done some kind of x86/x64 “alignment” for the projects in the Configuration Manager. If i select x86 as solution platform i will set Sandbox to x86, Core to Win32 and Wrapper to Win32. If i select x64 as solution platform i will set Sandbox, Core and Wrapper to x64

I had problems to run the solution on x86. The problem was that Wrapper and Sandbox used different .NET-versions. So now, if both use the same .NET-version (for example .NET 3.1 or .NET 5) for Wrapper and Sandbox, then i can run the solution on x86.

I have problems running the solution on x64. When i run it i get this error:

Microsoft Visual Studio

The debug executable
'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\bin\x64\Debug\net5.0\Sandbox.exe'
specified in the 'Sandbox' debug profile does not exist.

OK

I also get these Errors:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)    Core    C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Core\MSVCRTD.lib(exe_main.obj) 1   
Error   C1010   unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?   Wrapper C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Wrapper\Entity.cpp 16  
Error   CS0246  The type or namespace name 'Entity' could not be found (are you missing a using directive or an assembly reference?)    Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 9   Active
Error   CS0246  The type or namespace name 'Entity' could not be found (are you missing a using directive or an assembly reference?)    Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 9   Active
Error   CS0246  The type or namespace name 'CLI' could not be found (are you missing a using directive or an assembly reference?)   Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 2   Active
Error   CS0006  Metadata file 'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Wrapper.dll' could not be found   Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\CSC    1   Active
Error   LNK1120 1 unresolved externals  Core    C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Core.exe 1   

How to be able to run this solution as x64? What i am doing wrong?

EDIT1:

As Alan Birtles suggested, i post the text from the Output-window:

Build started...
1>------ Build started: Project: Core, Configuration: Debug x64 ------
Restored C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Sandbox.csproj (in 3 ms).
1>Entity.cpp
1>MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
1>C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Core.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Core.vcxproj" -- FAILED.
2>------ Build started: Project: Wrapper, Configuration: Debug x64 ------
2>pch.cpp
2>AssemblyInfo.cpp
2>Entity.cpp
2>C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Wrapper\Entity.cpp(16,1): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?
2>Wrapper.cpp
2>Generating Code...
2>Done building project "Wrapper.vcxproj" -- FAILED.
3>------ Build started: Project: Sandbox, Configuration: Debug x64 ------
3>CSC : error CS0006: Metadata file 'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Wrapper.dll' could not be found
3>Done building project "Sandbox.csproj" -- FAILED.
========== Build: 0 succeeded, 3 failed, 0 up-to-date, 0 skipped ==========

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文