Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include! can't read header when cxx is missing from dependencies #222

Closed
alexxbb opened this issue Jun 21, 2020 · 5 comments
Closed

include! can't read header when cxx is missing from dependencies #222

alexxbb opened this issue Jun 21, 2020 · 5 comments

Comments

@alexxbb
Copy link

alexxbb commented Jun 21, 2020

Hi! I'm having issues with including a c++ header file and I'd appreciate your help :)

#[cxx::bridge]
mod ffi {
    extern "C" {
        include!("cpp/wrap/wrap.h");
        type Spref;
        fn new_spref(name: &str) -> UniquePtr<Spref>;
    }
}

Note the "src/cpp/wrap/wrap.h"

error: couldn't read src/cpp/wrap/wrap.h: No such file or directory (os error 2)
 --> src/lib.rs:4:9
  |
4 |         include!("cpp/wrap/wrap.h");
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |

Here's my builld.rs

fn main() {
    cxx_build::bridge("src/lib.rs")  // returns a cc::Build
        .file("cpp/wrap/wrap.cpp")
        .include("cpp/lib")
        .flag_if_supported("-std=c++14")
        .compile("cpp-wrap");

    println!("cargo:rerun-if-changed=src/lib.rs");
    println!("cargo:rerun-if-changed=cpp/wrap/wrap.h");
    println!("cargo:rerun-if-changed=cpp/wrap/wrap.cpp");
}

If I change the path to something like:

include!("../cpp/wrap/wrap.h")

I'll get a different error:

running: "c++" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-shared" "-I" "/home/alex/Sandbox/rust/cxx_test/target/cxxbridge" "-I" "/home/alex/Sandbox/rust/cxx_test" "-I" "cpp/lib" "-Wall" "-Wextra" "-std=c++14" "-o" "/home/alex/Sandbox/rust/cxx_test/target/debug/build/cxx_test-38d75340743cc24c/out/src/lib.rs.o" "-c" "/home/alex/Sandbox/rust/cxx_test/target/debug/build/cxx_test-38d75340743cc24c/out/src/lib.rs.cc"
cargo:warning=/home/alex/Sandbox/rust/cxx_test/target/debug/build/cxx_test-38d75340743cc24c/out/src/lib.rs.cc:1:10: fatal error: ../cpp/wrap/wrap.h: No such file or directory
cargo:warning= #include "../cpp/wrap/wrap.h"
cargo:warning=          ^~~~~~~~~~~~~~~~~~~~
cargo:warning=compilation terminated.
exit code: 1

Thx,
Alex

@dtolnay
Copy link
Owner

dtolnay commented Jun 21, 2020

I think this means cxx::bridge is not being invoked. Does your Cargo.toml have cxx under [dependencies] and does it have edition = "2018" under [package]?

@alexxbb
Copy link
Author

alexxbb commented Jun 21, 2020

Oh you're right, I only added cxx-build but totally forgot to add cxx itself. It's now compiled. Thanks @dtolnay

@alexxbb alexxbb closed this as completed Jun 21, 2020
@alexxbb
Copy link
Author

alexxbb commented Jun 21, 2020

I stumbled upon another related issue:

I created a new cargo bin project alongside the cxx_test (which is a lib crate)
Cargo.toml:

[dependencies]
cxx_test = {path = ".."}

Building this gives me an error:

running: "c++" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-shared" "-I" "/home/alex/Sandbox/rust/cxx_test/consume/target/cxxbridge" "-I" "/home/alex/Sandbox/rust/cxx_test/consume" "-I" "cpp/lib" "-Wall" "-Wextra" "-std=c++14" "-lcxxlib" "-o" "/home/alex/Sandbox/rust/cxx_test/consume/target/debug/build/cxx_test-b3a77f1cdae677d0/out/src/lib.rs.o" "-c" "/home/alex/Sandbox/rust/cxx_test/consume/target/debug/build/cxx_test-b3a77f1cdae677d0/out/src/lib.rs.cc"
cargo:warning=/home/alex/Sandbox/rust/cxx_test/consume/target/debug/build/cxx_test-b3a77f1cdae677d0/out/src/lib.rs.cc:1:10: fatal error: cpp/wrap/wrap.h: No such file or directory
cargo:warning= #include "cpp/wrap/wrap.h"
cargo:warning=          ^~~~~~~~~~~~~~~~~
cargo:warning=compilation terminated.
exit code: 1

I guess this has something to do with include! uses a relative path of the cxx_test crate. Is there a way to fix it?

@alexxbb alexxbb reopened this Jun 21, 2020
@alexxbb
Copy link
Author

alexxbb commented Jun 21, 2020

Adding .include(".") to the build script fixes the problem.
I wish there was more examples and explanation of how things work. In particular it' be very helpful to see an example of how to write a simple binding to a c++ shared library.

@alexxbb alexxbb closed this as completed Jun 21, 2020
@dtolnay
Copy link
Owner

dtolnay commented Jun 21, 2020

Glad you got it working! As you noticed, the Cargo workflow is not well trodden yet. So far I have mostly been developing using Bazel, and using Buck at work. We are looking at making the Cargo workflow more robust in #214 and of course documenting it better in #179.

@dtolnay dtolnay changed the title c++ header include issue include! can't read header when cxx is missing from dependencies Aug 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants