2

Context:
I have a local C library called 'libmaths' that then uses Bindgen to create a 'libmaths-sys' crate that is locally stored in the same directory as my project.

Issue:
I want to use some of the features in the 2021 edition of Rust and currently my project is built off 2018. I am trying to update the project by following the instructions at:

https://doc.rust-lang.org/cargo/commands/cargo-fix.html

Run cargo fix --edition. Consider also using the --all-features flag if your project has multiple features. You may also want to run cargo fix --edition multiple times with different --target flags if your project has platform-specific code gated by cfg attributes.

Modify Cargo.toml to set the edition field to the new edition.

Run your project tests to verify that everything still works. If new warnings are issued, you may want to consider running cargo fix again (without the --edition flag) to apply any suggestions given by the compiler.

To run cargo fix --edition I am told by the compiler to remove the edition="2018" in cargo toml. Following this I receive a compile error stating that libmaths-sys cannot be found. The code compiles and executes normally in 2018 but not without this edition tag.

I can not find anyone with a similar issue, this is my first stackoverflow question so not sure how best to show my code given its a context of a small project.

Error code

error[E0432]: unresolved import `libmaths_sys`

 --> src/main.rs:1:5
  |
1 | use libmaths_sys::*; // lib.rs in sys crate
  |     ^^^^^^^^^^^^ maybe a missing crate `libmaths_sys`?

File Structure and general overview of project

.
├── Cargo.lock
├── Cargo.toml
├── libmaths
│   ├── add.c
│   ├── add.h
│   └── subtract.c
├── libmaths-sys
│   ├── build.rs
│   ├── Cargo.lock
│   ├── Cargo.toml
│   ├── src
│   │   └── lib.rs
│   └── wrapper.h
├── README.md
└── src
    ├── lib.rs
    └── main.rs

libmaths contains add.c that returns a + b and subtract.c which returns a - b, with a header add.h directing to both .c files

The Rust code generated by bindgen is attached via lib.rs in the libmath-sys crate which links to the OUT DIR which I have omitted from the tree to save 200 lines of file names.

Jambalaya
  • 31
  • 4

2 Answers2

2

Try updating edition="2018" to edition="2021"; otherwise it defaults to edition="2015" which requires usage of extern crate.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
  • I receive this error when I try that: error: failed to parse manifest at `.../project/Cargo.toml` Caused by: failed to parse the `edition` key Caused by: this version of Cargo is older than the `2021` edition, and only supports `2015` and `2018` editions. – Jambalaya Mar 02 '22 at 13:45
  • 1
    @Jambalaya Try updating Cargo first, then. To be able to use the `2021` edition, you'll need to anyways. If you're using Rustup, [`rustup update`](https://rust-lang.github.io/rustup/basics.html#keeping-rust-up-to-date) should be enough. – Solomon Ucko Mar 02 '22 at 14:12
  • I run rustup and this is the current version, the error is still the same when I try and run it. info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu' info: syncing channel updates for '1.48-x86_64-unknown-linux-gnu' info: checking for self-updates stable-x86_64-unknown-linux-gnu unchanged - rustc 1.59.0 (9d1b2106e 2022-02-23) 1.48-x86_64-unknown-linux-gnu unchanged - rustc 1.48.0 (7eac88abb 2020-11-16) info: cleaning up downloads & tmp directories – Jambalaya Mar 02 '22 at 14:43
1

As @Solomon Ucko directed me to, rustup update held the key.

Running rustup update produced:
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: syncing channel updates for '1.48-x86_64-unknown-linux-gnu'
info: checking for self-updates

  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.59.0 (9d1b2106e 2022-02-23)
    1.48-x86_64-unknown-linux-gnu unchanged - rustc 1.48.0 (7eac88abb 2020-11-16)

info: cleaning up downloads & tmp directories

In the end, rustup was using the old 1.48 version and not the installed 1.59 version.
To switch to the newer vesion I ran:

rustup default stable

I then could follow the instructions from the link in the original question to change the edition.

Jambalaya
  • 31
  • 4