Rust-bindgen: libbindgen: can't build opencv 3

Created on 14 Dec 2016  路  3Comments  路  Source: rust-lang/rust-bindgen

I'm not sure if it's possible and how to do it.

rustc 1.15.0-nightly (daf8c1dfc 2016-12-05)
libbindgen = "0.1.2"
clang 3.9.0-3
Arch Linux 64-bit

wrapper.h

#include <opencv2/core/core.hpp>

build.rs

extern crate libbindgen;

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

fn main() {
    // Tell cargo to tell rustc to link the system bzip2 shared library.
    println!("cargo:rustc-link-lib=opencv_core");

    // The libbindgen::Builder is the main entry point to libbindgen, and lets
    // you build up options for the resulting bindings.
    let bindings = libbindgen::Builder::default()
    // Do not generate unstable Rust code that requires a nightly rustc and
    // enabling unstable features.
        .no_unstable_rust()
    // The input header we would like to generate bindings for.
        .header("wrapper.h")
    // Finish the builder and generate the bindings.
        .generate()
    // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

cargo build

$ cargo build
Blocking waiting for file lock on build directory
Compiling opencv v0.1.0 (file:///home/bbigras/dev/rust/rs-opencv)
error: failed to run custom build command for opencv v0.1.0 (file:///home/bbigras/dev/rust/rs-opencv)
process didn't exit successfully: /home/bbigras/dev/rust/rs-opencv/target/debug/build/opencv-bf020cc2abd762ca/build-script-build (exit code: 101)
--- stdout
cargo:rustc-link-lib=opencv_core
/usr/include/opencv2/core.hpp:49:4: error: core.hpp header must be compiled as C++, err: true
/usr/include/opencv2/core/base.hpp:49:4: error: base.hpp header must be compiled as C++, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/bits/stl_relops.h:67:1: error: unknown type name 'namespace', err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/bits/stl_relops.h:67:43: error: expected ';' after top level declarator, err: true
/usr/include/opencv2/core/cvstd.hpp:48:4: error: cvstd.hpp header must be compiled as C++, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cstring:71:1: error: unknown type name 'namespace', err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cstring:71:43: error: expected ';' after top level declarator, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cctype:62:1: error: unknown type name 'namespace', err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cctype:62:14: error: expected ';' after top level declarator, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cwchar:62:1: error: unknown type name 'namespace', err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cwchar:62:14: error: expected ';' after top level declarator, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/clocale:51:1: error: unknown type name 'namespace', err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/clocale:51:14: error: expected ';' after top level declarator, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/x86_64-pc-linux-gnu/bits/gthr.h:151:13: error: #pragma visibility pop with no matching #pragma visibility push, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/ext/atomicity.h:38:1: error: unknown type name 'namespace', err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/ext/atomicity.h:38:49: error: expected ';' after top level declarator, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cmath:77:8: error: expected identifier or '(', err: true
/usr/include/opencv2/core/cvstd.hpp:68:1: error: unknown type name 'namespace', err: true
/usr/include/opencv2/core/cvstd.hpp:68:13: error: expected ';' after top level declarator, err: true
fatal error: too many errors emitted, stopping now [-ferror-limit=], err: true

"error: core.hpp header must be compiled as C++, err: true" is caused by "__cplusplus" not being defined. I'm not sure if I'm supposed to define it myself but if I do, I get:

   Compiling opencv v0.1.0 (file:///home/bbigras/dev/rust/rs-opencv)
error: failed to run custom build command for `opencv v0.1.0 (file:///home/bbigras/dev/rust/rs-opencv)`
process didn't exit successfully: `/home/bbigras/dev/rust/rs-opencv/target/debug/build/opencv-bf020cc2abd762ca/build-script-build` (exit code: 101)
--- stdout
cargo:rustc-link-lib=opencv_core
/usr/include/features.h:210:42: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib/clang/3.9.0/include/limits.h:92:48: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/x86_64-pc-linux-gnu/bits/c++config.h:81:43: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/x86_64-pc-linux-gnu/bits/c++config.h:93:16: error: expected value in expression, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/x86_64-pc-linux-gnu/bits/c++config.h:524:17: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/x86_64-pc-linux-gnu/bits/c++config.h:558:17: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/x86_64-pc-linux-gnu/bits/c++config.h:1561:17: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib/clang/3.9.0/include/stddef.h:117:48: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cstddef:52:17: error: invalid token at start of a preprocessor expression, err: true
/usr/include/opencv2/core/hal/interface.h:37:42: error: invalid token at start of a preprocessor expression, err: true
/usr/include/stdlib.h:34:1: error: expected identifier or '(', err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cstdlib:82:17: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cstdlib:103:17: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cstdlib:118:8: error: expected identifier or '(', err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cstdlib:130:17: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/cstdlib:153:17: error: invalid token at start of a preprocessor expression, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/stdlib.h:38:1: error: unknown type name 'using', err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/stdlib.h:38:10: error: expected ';' after top level declarator, err: true
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/6.2.1/../../../../include/c++/6.2.1/stdlib.h:38:11: error: expected identifier or '(', err: true
fatal error: too many errors emitted, stopping now [-ferror-limit=], err: true

Most helpful comment

A couple things to try:

  • rename wrapper.h to wrapper.hpp
  • In the builder, add .clang_arg("-x=c++").clang_arg("-std=c++14") (or whatever C++ standard you're targeting)

Do either of these help?

All 3 comments

A couple things to try:

  • rename wrapper.h to wrapper.hpp
  • In the builder, add .clang_arg("-x=c++").clang_arg("-std=c++14") (or whatever C++ standard you're targeting)

Do either of these help?

Renaming wrapper.h to wrapper.hpp worked. Thanks!

Nice blog article by the way.

Great! Thank you :)

Was this page helpful?
0 / 5 - 0 ratings