0

I have existing C code and its header and I need to call the C code from Rust. I tried it many ways and referred to documents but I didn't understand how to apply that to my code. I'm facing difficulties converting C functions into Rust. Please help me with some examples so that I can understand easily.

I tried to use the examples given in the Rust book and other website examples, but no resource has more details on this.

C_code.h

void ifx_vec_init_r(ifx_Vector_R_t* vector,
                    ifx_Float_t* d,
                    uint32_t length);

void ifx_vec_init_c(ifx_Vector_C_t* vector,
                    ifx_Complex_t* d,
                    uint32_t length);

void ifx_vec_rawview_r(ifx_Vector_R_t* vector,
                       ifx_Float_t* d,
                       uint32_t length,
                       uint32_t stride);

void ifx_vec_sub_r(const ifx_Vector_R_t* v1,
                   const ifx_Vector_R_t* v2,
                   ifx_Vector_R_t* result);

I want to call all above functions in Rust, but I am not able to understand how to start or how to change. What and all we to take care to wrap this C code? What are the ways available?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ganesh
  • 43
  • 1
  • 6
  • yes I did, But There is no detailed resources available. – ganesh Nov 05 '19 at 10:37
  • @ganesh [there is this resource](https://doc.rust-lang.org/nomicon/ffi.html). Not sure how you missed it. – Sébastien Renauld Nov 05 '19 at 10:46
  • And the [Rust FFI Omnibus](http://jakegoulding.com/rust-ffi-omnibus/) – Shepmaster Nov 05 '19 at 13:50
  • It looks like your question might be answered by the answers of [How to call C function in Rust](https://stackoverflow.com/q/57353387/155423); [Can I call C or C++ functions from Rust code?](https://stackoverflow.com/q/24105186/155423); etc. If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Nov 05 '19 at 13:52

1 Answers1

0

If you're trying to call C code from Rust, you need to create FFI bindings as some people have mentioned in the comments.

However, it is usually best to do this via rust-bindgen, which automatically generates the bindings and includes tests to make sure that the results are properly sized, aligned, etc. It is very easy to create a type with an incorrect size, which will give you no warning at compile-time and can cause undefined behavior unless you generate tests for them with bindgen.

Coder-256
  • 5,212
  • 2
  • 23
  • 51
  • Is Rust-Bindgen really works smoothly in Rust stable version? Because I am getting continues errors in all steps. How about Rust nightly version? which is preferred? – ganesh Nov 07 '19 at 13:42
  • It has worked for me in the past with stable Rust. What error are you getting? – Coder-256 Nov 08 '19 at 10:33
  • error: failed to run custom build command for `bindgen-tutorial-bzip2-sys v0.1.0 (C:\Users\Hegde\Desktop\bindgen-tutorial-bzip2-sys). --- stderr wrapper.h:1:10: fatal error: 'bzlib.h' file not found wrapper.h:1:10: fatal error: 'bzlib.h' file not found, err: true – ganesh Nov 08 '19 at 10:58
  • Make sure you have the bzip library in your include path, and I would open another question if you need further help. Hope this helps – Coder-256 Nov 08 '19 at 14:35
  • Thank you @Coder256 for your help, I was tried adding libraries but Error is remain same, Today once again I cloned it from official bindgen website but its doesn't change anything. If possible please open question. – ganesh Nov 11 '19 at 13:55
  • @ganesh I meant to just ask another question with the "Ask Question" button. Make sure to paste the error message and your code (also highlight it and click the `{}` button so it will look better). If you comment a link to the new question here I would be glad to help. – Coder-256 Nov 12 '19 at 15:38
  • Thank you, error got solved yesterday. Latest version of clang 0.51.1 has regression on that we need to to downgrade the version to 0.46.0 – ganesh Nov 13 '19 at 07:25