4

I am successfully using a .c file with Rust (see this answer). How can I link multiple .c files? 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 a better question would be, how can I simply drop the C source code in my Rust directory and start using it directly by using Rust's link attribute & extern (or any other way) with the above example in mind?

0

2 Answers 2

3

how can I simply drop the C source code in my Rust directory and start using it directly by using Rust's link attribute & extern (or any other way)

You can't just put C code into Rust sources and expect it to work. As said in the comments of your other question, the syntax #![link_args="foo.c"] has never be planned to work, and you should not rely on that.

The only thing Rust code can be linked against (except other rust code) is a compiled library (static or dynamic). You need to compile your C files into one or several libraries before being able to call them from your Rust code.

The proper way to do so is adding a build argument in the [package] section of your Cargo.toml, you can thus invoke a makefile that will first build your C files into a static library, and then link your Rust code against it.

All details are provided on the cargo website : http://doc.crates.io/build-script.html

Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you're using Cargo, add some sort of script with build = ["gcc ..."] to build them into a proper library, then you only have one thing to specify in #[link_args]

3 Comments

Yes, I am using Cargo. add some sort of script with build = ["gcc ..."] to.. Could you be a bit more specific please? Also, is this documented anywhere?
On the cargo website : crates.io/native-build.html
@goo, you can fine info on how to build non-Rust dependencies here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.