Using multiple .c source files with Rust

I am using a file successfully .c

with Rust ( see this answer ). How can I link multiple files .c

? I've already tried #![link_args="/c_src/*.c"]

with no luck.

- rust-demo
  - src
    - c_src
      - file1.c
      - file2.c
      - etc...
    - main.rs

      

Edit:

I think the best question is, how can I just delete the C source code in my Rust directory and start using it directly using the Rust attribute link

and extern

(or any other way) with the above example in mind?

+3


source to share


2 answers


how can I just delete the C source code in my Rust directory and start using it directly using the Rust attribute link

and extern

(or any other way)

You can't just type C code into the Rust sources and expect it to work. As stated in the comments to your other question, the syntax is #![link_args="foo.c"]

never planned to work and you shouldn't rely on it.

The only thing that Rust code can be tied to (other than other rust code) is a compiled library (static or dynamic). You need to compile your C files into one or more libraries before you can call them from Rust code.



The correct way to do this is to add an argument build

in a section of [package]

yours Cargo.toml

, so you can call a makefile that will build your C files in a static library first and then link your code to rust against it.

All data is presented on the truck website: http://doc.crates.io/build-script.html

+3


source


Assuming you are using Cargo, add something like script with build = ["gcc ..."]

to build them in the correct library, then you only need to specify one thing in#[link_args]



+1


source







All Articles