c2nim
ClangSharp
Our great sponsors
c2nim | ClangSharp | |
---|---|---|
7 | 5 | |
490 | 855 | |
0.4% | 2.6% | |
4.1 | 8.1 | |
5 months ago | about 19 hours ago | |
Nim | C# | |
MIT License | MIT License |
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.
c2nim
-
I learned 7 programming languages so you don't have to
Also on the game development front, I maintain a raylib wrapper https://github.com/planetis-m/naylib As long utilities like c2nim https://github.com/nim-lang/c2nim exist, it's trivial to create bindings of C/C++ libraries. One thing I want to experiment more is making it more automatic by writing a callback exposed by c2nim that transform the generated code using Nim's AST. But regardless in that project I was able to write safe language abstractions on top of the bindings that provide a more native experience. It has scope-based memory management, generics and ... function overloading.
-
The way integrate Rust into Nim
Rust also has a library called safer_ffi that makes FFI easier, and I tried to use that, but the library seems to be immature, and I could not get arguments in Rust functions. If this library can be used properly, it will be possible to output C header files from Rust functions and automatically generate Nim interface functions from C header files using c2nim. We look forward to further development of this feature.
-
Checked C
Well I'm 99.5% certain at least. Even now I'm uncertain of the C syntax. And I've not been bold enough to test 3rd order C function pointers. I figure that's probably C code you don't wanna touch if possible.
https://github.com/nim-lang/c2nim/blob/11f2c5363dfe7e8c7c8ce...
The other annoying one is that "signed" and "unsigned" are basically adjectives, but "long" can be both a type and a modifier. So it's difficult to parse unless you're the target C compiler. Technically you can, but you have to use backtracking.
-
Nim -- a modern "glue" language like Python
c2nim is a tool to translate ANSI C code to Nim. The output is human-readable Nim code that is meant to be tweaked by hand after the translation process. If you are tired of wrapping C library, you can try futhark which supports "simply import C header files directly into Nim". Similar to futhark, cinterop allows one to interop with C/C++ code without having to create wrappers. nimLUA is a glue code generator to bind Nim and Lua together using Nim's powerful macro. nimpy and nimporter is a bridge between Nim and Python. rnim is a bridge between R and Nim. nimjl is a bridge between Nim and Julia! Last but not least, genny generates a shared library and bindings for many languages such as Python, Node.js, C.
-
Genny – Generate Nim library bindings for many languages
Now if only a full binding generator for C and C++ headers to Nim was done, the language would really be cooking!
"What do you mean, Nim has two of these already?"
Yeah, I know, and -- not to hurt anyone's feelings -- but: they kind of suck. And there's no way I see them able to be extended to do the job fully, based on the way they're currently built.
Those are some bold claims to make!
So before I get stoned to death (no offense to the authors, I am grateful that they exist and have used them both) let me attempt an explanation and back up these statements.
---
To start off, the two tools available are "c2nim" and "nimterop". c2nim is a Nim official library, while nimterop is a community library.
https://github.com/nim-lang/c2nim
-
ffipf - quickly jump to file in a project with a native module
Yes. Nim compiles to C and has a lot of features for interacting with C code. You can easily call C routines from Nim and Nim functions from C. There's a bit of a ceremony you need to go through, but most of it can be automated with c2nim tool: https://github.com/nim-lang/c2nim
- C2nim: A tool to translate ANSI C code to Nim
ClangSharp
-
The future of Clang-based tooling
One of the most surprising things I learned about "clang" was how relatively poor the "libClang" capabilities are.
I wanted to write a codegen tool that would auto-generate bindings for C++ code, and it turns out that "libTooling" is the only reasonable way to get access to the proper info you need from C++.
Another alternative is "libClangSharp", from Tanner Gooding who works on C# at Microsoft.
-
How to make C# bindings for C++ libs
Bindings for C++ are tricky due to function name mangling and also due to ABI compat, it's basically impossible to interop with a std::vector for example. Afaik the most common approach is to PInvoke thin C wrappers around the C++ functions. There are tons of binding generators for this but they aren't without problems (not worth if you have a small API surface).
-
C# calling C Shared Library
3: Throwing my work away and using ClangSharpto create the bindings, unfortunately the header file for the shared library lists other C library's that I can not find online such as > #include
-
Genny – Generate Nim library bindings for many languages
I've found this to be true, with the exception that a few people have taken to "augmenting" libclang with extra bindings to libTooling functions or other C++-only methods.
Most notably is Tanner Gooding & Co's "libClangSharp" from the Microsoft "ClangSharp" project, and PathogenDavid's "Biohazrd" libraries (which again are a sort of enhanced fork based on ClangSharp):
https://github.com/microsoft/ClangSharp # See "/sources/libClangSharp"
-
How can I use C definitions in .NET 5?
If you want to go down that route, clangsharp can do this for you.
What are some alternatives?
nimterop - Nimterop is a Nim package that aims to make C/C++ interop seamless
CsWin32 - A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
futhark - Automatic wrapping of C headers in Nim
genny - Generate a shared library and bindings for many languages.
nimLUA - glue code generator to bind Nim and Lua together using Nim's powerful macro
Biohazrd - A framework for automatically generating binding wrappers for C/C++ libraries
cinterop - A C/C++ interop library for the Nim programming language
cakeml - CakeML: A Verified Implementation of ML
tinycc - Unofficial mirror of mob development branch
binder - Binder, tool for automatic generation of Python bindings