C API via LLVM
Hi All,
Has anybody tried to create msp object via the C API using a LLVM based language like Rust or Kotlin?
I have not, but am interested in this!
It seems theoretically possible to call Rust functions from C, but I haven't tried it:
https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html#using-extern-functions-to-call-external-code
I could only find one bit of information about creating a max external using rust. A reddit user casually mentioned in a longer post, that they wrote a simple Max external in Rust. I found that post again and messaged the user to ask for more information, and here is their reply:
(reposted here with their permission)
Hey, I don't know how much this will help you out, because I haven't touched Rust since 1.0 came out, and this was written prior to that, also this was back in Max 6 days, also if it matters, this was compiled on OSX, so it's a "Bundle" like other plugins and apps are.
I dug around through a bunch of stuff until I found the rust source I got to work in Max, maybe it'll help somewhat, not sure, it's really just a max object that responds to bang, float, int, etc, but there's no real reason you can't just write whatever parts you want in Rust, and probably MSP also.
Basically what it is is a C file that registers the max object using functions that were compiled separately by rustc as a .o file, so all the functionality is in Rust, just using a C entry point that registers the object.
I'm not even sure it is necessary to have the little C entrypoint but it certainly helped get it working.
The Makefile included just compiles the C entrypoint to a .o, and then asks rustc to make a staticlib .o file, and then uses gcc to do the rest of the linking etc.
In the C file I defined the Max object's functions extern, and then wrote them in Rust, and used the #[no mangle] pragma thing so the symbol name and calling convention would work, ie CDECL.
I started going through the Max header files and rewriting the structs in Rust also so I could just use them since I didn't have Max header files for Rust, and I defined some functions from the max library/head extern also.
Anyways, I have no clue if this will help at all, kinda surprised I still had it, but this did work. Here is the source.
Keep in mind that most non-standard system languages come with their own runtimes, which enable them to do all that fancy stuff with data structs and memory management but also introduce some overhead. They have to be compiled in or provided and linked with an external. Exporting c-lib, writing wrapper, debugging, debugging, then debugging... IMHO it's not worth it, unless you have a good reason.
(A nice syntax is not "a good reason").
Something I've been working on in my spare time: https://github.com/Cycling74/median/tree/develop/median
Rust bindings for the Max SDK and wrapper/glue code/macros for writing externals.