Bindgen basic help

Hi,
I'm trying to use bindgen to create bindings to C++ code (kea dhcp server).
I'm following guides but I've stumbled on first step.. :slight_smile:
My Cargo.toml contains required declarations:

[package]
build = "build.rs"

[dependencies]
bindgen = "0.26.1"

build.rs in my crate root folder have content according to example from bindgen:

extern crate bindgen;

use std::env;
use std::path::PathBuf;

fn main() {
// code here

And I'm getting error when running cargo build:

error[E0463]: can't find crate for `bindgen`
 --> build.rs:1:1
  |
1 | extern crate bindgen;
  | ^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to previous error

What I'm doing wrong ?

Put it in [build-dependencies] like https://github.com/compass-rs/sass-rs/pull/11/files#diff-3f5590e43a4a71147163b6b694dc7c66R16

Ha,

This helped. Thanks !

Why such information is not present in any bindgen doc/example ?

I just had a skim through their guide and found this.

Still now working. already kept it in [build-dependencies]

It works fine on my machine.

Can you share the code or error messages?

error[E0463]: can't find crate for bindgen
--> build.rs:1:1
|
1 | extern crate bindgen;
| ^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to previous error

in Cargo.toml:

[build-dependencies]
bindgen = "0.53.1"

That's odd, it works for me.

$ cargo --version
cargo 1.51.0-nightly (a73e5b7d5 2021-01-12)
$ rustc --version --verbose
rustc 1.51.0-nightly (c5a96fb79 2021-01-19)
binary: rustc
commit-hash: c5a96fb7973649807a7943e7395456db158dcab6
commit-date: 2021-01-19
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1

$ cargo new --lib repro
$ cd repro

$ cat Cargo.toml
[package]
name = "repro"
version = "0.1.0"
authors = ["Michael-F-Bryan <michaelfbryan@gmail.com>"]
edition = "2018"
build = "build.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[build-dependencies]
bindgen = "0.56.0"

$ cat build.rs
extern crate bindgen;

fn main() { }

$ cat src/lib.rs
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

$ cargo build
   Compiling repro v0.1.0 (/tmp/repro)
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s

Do i need to "cargo build" or rustc build.rs. cargo build gives different errors

cannot find macro llvm_asm in this scope
/mod.rs:16:5
|
16 | llvm_asm!("syscall"
| ^^^^^^^^

strong text

cargo --version
cargo 1.43.0
$ rustc --version --verbose
rustc 1.43.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.43.0
LLVM version: 9.0

You should be building with cargo build. Cargo is the Rust package manager and looks after downloading dependencies and making sure they get compiled into your program.

For more on how to compile things I'd recommend reading the corresponding chapter from the book:

https://doc.rust-lang.org/book/ch01-03-hello-cargo.html

The llvm_asm!() macro was an unstable feature for telling LLVM (the thing inside Rust which generates machine code) to insert some assembly instructions. You'll need to opt into this unstable/incomplete feature it by adding #![feature(llvm_asm)] to the top of your lib.rs or main.rs.

I believe it's since been superseded by the asm!() macro.

should i just add #![feature(llvm_asm)] in the first line of lib.rs?