rust-lang / rust-lang/rust-bindgen

cannot build a rust file using std::complex

Open
#1,738 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-templates bug
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

I want to build an interface between c++ and rust, but different errors occurred.

Input C/C++ Header
#ifndef __TEST_HPP__
#define __TEST_HPP__

#include <cstdint>
#include <array>
#include <complex>

template<uint32_t s_>
class Config{
    uint16_t    doppler_idx;
    uint16_t    range_idx;
    std::array< std::complex<float> , s_> vector;
};

#endif

If I rename the types to

class Complex : std::complex<float>{}; 

seems to help, but then I do not have the functions, containing the std::complex parent class.

Bindgen Invocation
extern crate bindgen;

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

fn main() {
    let bindings = bindgen::Builder::default()
        .header("test.hpp")
        .clang_arg("-std=c++17")
        .clang_arg("-x")
        .clang_arg("c++")
        .opaque_type("std::.*")
        .generate()    
        .unwrap();

    // 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("test.rs"))
        .expect("Couldn't write bindings!");
}
Actual Results
       Fresh version_check v0.1.5
       Fresh cc v1.0.50
       Fresh glob v0.3.0
       Fresh lazy_static v1.4.0
       Fresh unicode-xid v0.2.0
       Fresh regex-syntax v0.6.14
       Fresh quick-error v1.2.3
       Fresh unicode-width v0.1.7
       Fresh cfg-if v0.1.10
       Fresh ansi_term v0.11.0
       Fresh termcolor v1.1.0
       Fresh vec_map v0.8.1
       Fresh strsim v0.8.0
       Fresh shlex v0.1.1
       Fresh peeking_take_while v0.1.2
       Fresh lazycell v1.2.1
       Fresh rustc-hash v1.1.0
       Fresh thread_local v1.0.1
       Fresh humantime v1.3.0
       Fresh textwrap v0.11.0
       Fresh libc v0.2.67
       Fresh memchr v2.3.3
       Fresh log v0.4.8
       Fresh proc-macro2 v1.0.9
       Fresh bitflags v1.2.1
       Fresh atty v0.2.14
       Fresh aho-corasick v0.7.8
       Fresh nom v4.2.3
       Fresh libloading v0.5.2
       Fresh quote v1.0.2
       Fresh which v3.1.0
       Fresh regex v1.3.4
       Fresh cexpr v0.3.6
       Fresh clap v2.33.0
       Fresh clang-sys v0.28.1
       Fresh env_logger v0.7.1
       Fresh bindgen v0.53.1
   Compiling test v0.1.0 (/mnt/c/Users/Anton/rust_ffi)
     Running `rustc --crate-name test src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --emit=dep-info,link -C debuginfo=2 --test -C metadata=8090ca622680a031 -C extra-filename=-8090ca622680a031 --out-dir /mnt/c/Users/Anton/rust_ffi/target/debug/deps -C incremental=/mnt/c/Users/Anton/rust_ffi/target/debug/incremental -L dependency=/mnt/c/Users/Anton/rust_ffi/target/debug/deps`
     Running `rustc --crate-name test src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=ea306d16b166d5b8 -C extra-filename=-ea306d16b166d5b8 --out-dir /mnt/c/Users/Anton/rust_ffi/target/debug/deps -C incremental=/mnt/c/Users/Anton/rust_ffi/target/debug/incremental -L dependency=/mnt/c/Users/Anton/rust_ffi/target/debug/deps`
error[E0428]: the name `FP_INT_UPWARD` is defined multiple times
     --> /mnt/c/Users/Anton/rust_ffi/target/debug/build/test-4a7a62747503f49c/out/test.rs:13105:1
      |
1000  | pub const FP_INT_UPWARD: u32 = 0;
      | --------------------------------- previous definition of the value `FP_INT_UPWARD` here

...

error[E0412]: cannot find type `_Tp` in this scope
    --> /mnt/c/Users/Anton/rust_ffi/target/debug/build/test-4a7a62747503f49c/out/test.rs:1250:27
     |
1250 |     pub static std_value: _Tp;
     |                           ^^^ not found in this scope

error[E0412]: cannot find type `_Value` in this scope
    --> /mnt/c/Users/Anton/rust_ffi/target/debug/build/test-4a7a62747503f49c/out/test.rs:6555:33
     |
6555 |     pub static __gnu_cxx___min: _Value;
     |                                 ^^^^^^ not found in this scope

error[E0412]: cannot find type `_Value` in this scope
    --> /mnt/c/Users/Anton/rust_ffi/target/debug/build/test-4a7a62747503f49c/out/test.rs:6559:33
     |
6559 |     pub static __gnu_cxx___max: _Value;
     |                                 ^^^^^^ not found in this scope

error[E0391]: cycle detected when processing `int_type`
     --> /mnt/c/Users/Anton/rust_ffi/target/debug/build/test-4a7a62747503f49c/out/test.rs:17321:21
      |
17321 | pub type int_type = int_type;
      |                     ^^^^^^^^
      |
      = note: ...which again requires processing `int_type`, completing the cycle
note: cycle used when collecting item types in top-level module
     --> src/lib.rs:1:1
      |
1     | / #![allow(non_upper_case_globals)]
2     | | #![allow(non_camel_case_types)]
3     | | #![allow(non_snake_case)]
4     | |
5     | | include!(concat!(env!("OUT_DIR"), "/test.rs"));
      | |_______________________________________________^

error: aborting due to 18 previous errors

Some errors have detailed explanations: E0391, E0412, E0428.
For more information about an error, try `rustc --explain E0391`.
error: could not compile `test`.

Caused by:
  process didn't exit successfully: `rustc --crate-name test src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C debuginfo=2 -C metadata=ea306d16b166d5b8 -C extra-filename=-ea306d16b166d5b8 --out-dir /mnt/c/Users/Anton/rust_ffi/target/debug/deps -C incremental=/mnt/c/Users/Anton/rust_ffi/target/debug/incremental -L dependency=/mnt/c/Users/Anton/rust_ffi/target/debug/deps` (exit code: 1)
warning: build failed, waiting for other jobs to finish...
error[E0412]: cannot find type `_Tp` in this scope
    --> /mnt/c/Users/Anton/rust_ffi/target/debug/build/test-4a7a62747503f49c/out/test.rs:1250:27
     |
1250 |     pub static std_value: _Tp;
     |                           ^^^ not found in this scope

error[E0412]: cannot find type `_Value` in this scope
    --> /mnt/c/Users/Anton/rust_ffi/target/debug/build/test-4a7a62747503f49c/out/test.rs:6555:33
     |
6555 |     pub static __gnu_cxx___min: _Value;
     |                                 ^^^^^^ not found in this scope

error[E0412]: cannot find type `_Value` in this scope
    --> /mnt/c/Users/Anton/rust_ffi/target/debug/build/test-4a7a62747503f49c/out/test.rs:6559:33
     |
6559 |     pub static __gnu_cxx___max: _Value;
     |                                 ^^^^^^ not found in this scope

error[E0391]: cycle detected when processing `int_type`
     --> /mnt/c/Users/Anton/rust_ffi/target/debug/build/test-4a7a62747503f49c/out/test.rs:17321:21
      |
17321 | pub type int_type = int_type;
      |                     ^^^^^^^^
      |
      = note: ...which again requires processing `int_type`, completing the cycle
note: cycle used when collecting item types in top-level module
     --> src/lib.rs:1:1
      |
1     | / #![allow(non_upper_case_globals)]
2     | | #![allow(non_camel_case_types)]
3     | | #![allow(non_snake_case)]
4     | |
5     | | include!(concat!(env!("OUT_DIR"), "/test.rs"));
      | |_______________________________________________^

error: aborting due to 18 previous errors

Some errors have detailed explanations: E0391, E0412, E0428.
For more information about an error, try `rustc --explain E0391`.
error: could not compile `test`.

Caused by:
  process didn't exit successfully: `rustc --crate-name test src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --emit=dep-info,link -C debuginfo=2 --test -C metadata=8090ca622680a031 -C extra-filename=-8090ca622680a031 --out-dir /mnt/c/Users/Anton/rust_ffi/target/debug/deps -C incremental=/mnt/c/Users/Anton/rust_ffi/target/debug/incremental -L dependency=/mnt/c/Users/Anton/rust_ffi/target/debug/deps` (exit code: 1)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with test.hpp and the shown bindgen Builder invocation, then inspect the generated test.rs included from src/lib.rs alongside the listed compiler errors. Trace how the std::complex and template declarations are represented; done means the generated bindings compile without the reported duplicate, missing-type, and recursive-alias errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.