Daa*_*aan 15 c# c++ cygwin cmake .net-core
我试图做同样的事情到这个:
我正在使用Windows,但我的目的是让我的代码在Linux上工作得太晚(因此我使用cygwin和clion for C++).VS2017使用普通的C#编译器为.NET Core应用程序编译C#.我的问题是在visual studio中出现此错误:
"该程序'[19944] dotnet.exe'已退出,代码为-1073741819(0xc0000005)'访问冲突'."
这是我的cmake文件(使用clion生成):
cmake_minimum_required(VERSION 3.10) project(callFromCsharp)
set(CMAKE_CXX_STANDARD 14)
add_library(callFromCsharp SHARED library.cpp)
Run Code Online (Sandbox Code Playgroud)
这是我在library.cpp中的C++代码:
#include <cstdint>
extern "C" __declspec(dllexport) int32_t Test(){
return 10;
}
Run Code Online (Sandbox Code Playgroud)
这是我的clion生成的cmake调用
C:\ Users\Daant.CLion2018.1\system\cygwin_cmake\bin\cmake.exe --build/cygdrive/c/Users/Daant/CLionProjects/callFromCsharp/cmake-build-release --target callFromCsharp - -j 6
这是我的C#代码:
class Program
{
[DllImport("cygcallFromCsharp.dll", EntryPoint = "Test", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern Int32 Test();
[STAThread]
static void Main()
{
var res = Test();
Console.WriteLine($"Done! {res}");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
怎么解决这个?我只想调用一个没有错误或异常的C++方法.
让我们从不该做什么开始
当从 C# 加载 Cygwin dll 时(我猜从 Visual studio 中它会是相同的)。
sprintf, sscanf, stringstream... 和打印到控制台方法会导致程序停止。现在你可以做什么:
#define EXPORT_API extern "C" __cdecl __declspec(dllexport)
g++ -c library.cpp; g++ -o cygcallFromCsharp.dll library.oDllImport(@"cygcallFromCsharp.dll", CallingConvention=CallingConvention.Cdecl)]
static extern int Test();
希望它会有所帮助。
| 归档时间: |
|
| 查看次数: |
456 次 |
| 最近记录: |