summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-03-19 22:52:58 +0800
committerruki <[email protected]>2026-03-19 22:52:58 +0800
commit879355cb0236593d8b73b4141f547585f37134e0 (patch)
treec7f5f437f4c06c7b428ee97874cae17ca7e6e8ae
parented0996a0ead18b79a3e279a200db0f9e13a4ec23 (diff)
csharp call c++ library
-rw-r--r--tests/projects/csharp/pinvoke/src/app/Program.cs12
-rw-r--r--tests/projects/csharp/pinvoke/src/native/mathlib.c13
-rw-r--r--tests/projects/csharp/pinvoke/test.lua13
-rw-r--r--tests/projects/csharp/pinvoke/xmake.lua10
-rw-r--r--xmake/rules/csharp/config.lua13
5 files changed, 61 insertions, 0 deletions
diff --git a/tests/projects/csharp/pinvoke/src/app/Program.cs b/tests/projects/csharp/pinvoke/src/app/Program.cs
new file mode 100644
index 000000000..b331bcb8d
--- /dev/null
+++ b/tests/projects/csharp/pinvoke/src/app/Program.cs
@@ -0,0 +1,12 @@
+using System.Runtime.InteropServices;
+
+[DllImport("mathlib")]
+static extern int add(int a, int b);
+
+[DllImport("mathlib")]
+static extern int multiply(int a, int b);
+
+int sum = add(3, 4);
+int product = multiply(5, 6);
+Console.WriteLine($"add(3,4)={sum}");
+Console.WriteLine($"multiply(5,6)={product}");
diff --git a/tests/projects/csharp/pinvoke/src/native/mathlib.c b/tests/projects/csharp/pinvoke/src/native/mathlib.c
new file mode 100644
index 000000000..3d2840073
--- /dev/null
+++ b/tests/projects/csharp/pinvoke/src/native/mathlib.c
@@ -0,0 +1,13 @@
+#ifdef _WIN32
+#define EXPORT __declspec(dllexport)
+#else
+#define EXPORT __attribute__((visibility("default")))
+#endif
+
+EXPORT int add(int a, int b) {
+ return a + b;
+}
+
+EXPORT int multiply(int a, int b) {
+ return a * b;
+}
diff --git a/tests/projects/csharp/pinvoke/test.lua b/tests/projects/csharp/pinvoke/test.lua
new file mode 100644
index 000000000..d909316b7
--- /dev/null
+++ b/tests/projects/csharp/pinvoke/test.lua
@@ -0,0 +1,13 @@
+import("detect.sdks.find_dotnet")
+
+function test_build(t)
+ if is_subhost("msys") then
+ return t:skip("csharp not supported on msys")
+ end
+ local dotnet = find_dotnet()
+ if dotnet and dotnet.sdkver then
+ t:build()
+ else
+ return t:skip("dotnet sdk not found")
+ end
+end
diff --git a/tests/projects/csharp/pinvoke/xmake.lua b/tests/projects/csharp/pinvoke/xmake.lua
new file mode 100644
index 000000000..db0dc7ebd
--- /dev/null
+++ b/tests/projects/csharp/pinvoke/xmake.lua
@@ -0,0 +1,10 @@
+add_rules("mode.debug", "mode.release")
+
+target("mathlib")
+ set_kind("shared")
+ add_files("src/native/*.c")
+
+target("app")
+ set_kind("binary")
+ add_deps("mathlib")
+ add_files("src/app/*.cs")
diff --git a/xmake/rules/csharp/config.lua b/xmake/rules/csharp/config.lua
index 3f6b71844..e9dbc71e0 100644
--- a/xmake/rules/csharp/config.lua
+++ b/xmake/rules/csharp/config.lua
@@ -44,4 +44,17 @@ function main(target)
end, {dependfile = dependfile, files = sourcefiles, values = depcsproj})
target:data_set("csharp.csproj", csprojfile)
+
+ -- add native shared library search paths for P/Invoke
+ for _, dep in ipairs(target:orderdeps()) do
+ if dep:is_shared() and not dep:rule("csharp.build") then
+ if is_host("windows") then
+ target:add("runenvs", "PATH", dep:targetdir())
+ elseif is_host("macosx") then
+ target:add("runenvs", "DYLD_LIBRARY_PATH", dep:targetdir())
+ else
+ target:add("runenvs", "LD_LIBRARY_PATH", dep:targetdir())
+ end
+ end
+ end
end