-
-
Notifications
You must be signed in to change notification settings - Fork 935
csharp call c++ library #7410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
csharp call c++ library #7410
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
879355c
csharp call c++ library
waruqi 930be83
add native aot
waruqi 82fdf6f
fix configure
waruqi b5cac03
improve configure
waruqi 826947f
fix windows issues
waruqi faac086
fix csharp aot
waruqi 2b0964d
remove native_aot
waruqi b9817aa
revert configure
waruqi 144688e
improve pinvoke
waruqi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}"); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string literal "mathlib" is repeated across multiple
DllImportattributes. Repeating such literals can lead to maintainability issues; if the native library's name changes, it would require updating multiple locations. It is a good practice to define such values as aconst stringin a dedicatedstaticclass for P/Invoke declarations to centralize the definition and improve code organization.For example, you could refactor these declarations into a
NativeMethodsclass:This approach enhances maintainability and provides a clear separation for native interop declarations.