cbindgen | A project for generating C bindings from Rust code
kandi X-RAY | cbindgen Summary
kandi X-RAY | cbindgen Summary
cbindgen creates c/c++11 headers for rust libraries which expose a public c api. while you could do this by hand, it's not a particularly good use of your time. it's also much more likely to be error-prone than machine-generated headers that are based on your actual rust code. the cbindgen developers have also worked closely with the developers of rust to ensure that the headers we generate reflect actual guarantees about rust's type layout and abi. c++ headers are nice because we can use operator overloads, constructors, enum classes, and templates to make the api more ergonomic and rust-like. c headers are nice because you can be more confident that whoever you're interoperating with can handle them. with cbindgen you don't need to choose! you can just tell it to emit both from the same rust library. there are two ways to use
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of cbindgen
cbindgen Key Features
cbindgen Examples and Code Snippets
Community Discussions
Trending Discussions on cbindgen
QUESTION
I have been trying to compile a simple rust cdylib
crate in windows and linking it with a simple c program. Despite all my efforts I fail to link the dll
file.
Minimal example
First of all my rustc
version is:
ANSWER
Answered 2021-Mar-15 at 18:08I think there are multiple problems here.
- You compile the library for a 64bit system (because Rust is
x86_64
), but you try to link it with 32bitMinGW
- Rust is using the
MSVC
toolchain and you try to compile the C program withMinGW
- I am not entirely sure about that, but I think you linked the
mycrate.dll.lib
incorrectly. According to that answer you should prefix it withl
like so:-L -lmycrate.dll.lib
Obviously there are multiple ways to fix that. For example you can install Rust, so that it uses the MinGW
toolchain as explained here.
Or you can use Visual Studio to compile your C code. That is what I did, because I did not want to bother with reinstalling Rust (Please correct me if am wrong, but I think there is no easy way to configure both MSVC
and MinGW
backends, so that one can switch easily between them in Rust).
The steps to compile and link with VisualStudio 2019 are as follows:
- Build the Rust project
cargo build --release
with your 64 bit Rust installation using MSVC - Create a new
Empty C++
project - Add
main.c
and insert your code - In the same directory where your solution file is placed put
headers/mycrate.h
- Copy
mycrate.dll
andmycrate.dll.lib
into the same directory where your Solution file is placed - Right click the C++ project in VisualStudio and select Properties
- Select
Configuration=Release
andPlatform=x64
- Add
$(SolutionDir)\headers;
toC/C++ -> General -> Additional Include Directories
- Add
$(SolutionDir)mycrate.dll.lib;
toLinker -> Input -> Additional Dependencies
- Apply all changes and close the properties pane
- Build the project in
Release
mode with thex64
platform
If you build the Rust project in debug mode (e.g. cargo build
) you will have to do the same for Debug
mode.
I know it is kind of a convoluted process, so let me know if I should make Repo that serves as a demo for that process...
As I said, if you like to build using MinGW
, you will have to setup Rust differently as explained here.
QUESTION
On the Rust side I wrote a function that returns a String as a pointer of bytes (laid out in memory as a C struct):
...ANSWER
Answered 2020-Mar-21 at 00:29After working through your code, you are redefining RustByteSlice
.
From Using Imported C Structs and Unions in Swift, you don't need to redefine it as it automatically imports the struct.
The below swift code works.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install cbindgen
A configuration (cbindgen.toml, which can be empty to start)
A Rust crate with a public C API
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page