So I'm trying to generate bindings for a C++ library, wxWidgets...
Here is my wrapper.hpp
:
#include <wx/wx.h>
Here is my build.rs
:
use std::path::PathBuf;
use std::process::Command;
fn main() {
println!("cargo:rustc-link-lib=wxWidgets");
println!("cargo:rerun-if-changed=wrapper.hpp");
// Beyond just the include paths, the flags returned by `wx-config` include
// important target definitions that tell wxWidgets what platform it's on,
// which means we really need those flags!
//
// Here's what they are on my system as an example:
// -I/usr/local/lib/wx/include/osx_cocoa-unicode-3.1
// -I/usr/local/include/wx-3.1 -DWXUSINGDLL -D__WXOSX_COCOA__ -D__WXMAC__
// -D__WXOSX__ -pthread
//
// Other than the include paths, that includes `WXUSINGDLL`,
// `__WXOSX_COCOA__`, `__WXMAC__`, and `__WXOSX__`! Also the include paths
// are kind of important to be able to detect dynamically!
let cxx_flags = Command::new("wx-config")
.arg("--cxxflags")
.output()
.expect("Couldn't get flags! Ensure you have `wx-config` in your PATH!");
let cxx_flags = String::from_utf8(cxx_flags.stdout).unwrap();
let cxx_flags = cxx_flags + " -std=c++14";
// It's apparently impossible to define this programmatically, so I have to
// abuse this environment variable meant to be used by the user!
std::env::set_var(
"BINDGEN_EXTRA_CLANG_ARGS",
std::env::var("BINDGEN_EXTRA_CLANG_ARGS")
.map(|str| str + " " + cxx_flags.as_ref())
.unwrap_or(cxx_flags)
);
let bindings = bindgen::Builder::default()
.header("wrapper.hpp")
.derive_default(true)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Couldn't make bindings!");
let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
bindings.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
I've managed to fix many issues with my compiler toolchain up to this point, but right now I have no idea what to do!
Here's the error it gives me:
/Users/LoganDark/.cargo/bin/cargo build --color=always
Compiling wxRust v0.1.0 (/Users/LoganDark/CLionProjects/wxRust/wxRust-sys)
error: failed to run custom build command for `wxRust v0.1.0 (/Users/LoganDark/CLionProjects/wxRust/wxRust-sys)`
Caused by:
process didn't exit successfully: `/Users/LoganDark/CLionProjects/wxRust/target/debug/build/wxRust-0a9230353732ad92/build-script-build` (exit code: 101)
--- stdout
cargo:rustc-link-lib=wxWidgets
cargo:rerun-if-changed=wrapper.hpp
--- stderr
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/wchar.h:89:10: fatal error: 'stdarg.h' file not found
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/wchar.h:89:10: fatal error: 'stdarg.h' file not found, err: true
thread 'main' panicked at 'Couldn't make bindings!: ()', wxRust-sys/build.rs:37:20
But, I know my LLVM_CONFIG_PATH is set to /usr/local/opt/llvm/bin/llvm-config
, so I can run it myself and find out what clang is being used!
0 LoganDark ~ /usr/local/opt/llvm/bin/llvm-config --bindir
/usr/local/Cellar/llvm/10.0.0_3/bin
And from that I can find out the include paths!
0 LoganDark ~ /usr/local/Cellar/llvm/10.0.0_3/bin/clang -E -Wp,-v -
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target x86_64-apple-darwin18.7.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/local/Cellar/llvm/10.0.0_3/lib/clang/10.0.0/include
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks (framework directory)
End of search list.
And from that I can find...
0 LoganDark ~ find /usr/local/Cellar/llvm/10.0.0_3/lib/clang/10.0.0/include -name stdarg.h
/usr/local/Cellar/llvm/10.0.0_3/lib/clang/10.0.0/include/stdarg.h
...
What?
stdarg.h
is right here! And clang
searches it by default!
Okay fine, let me set an environment variable CPLUS_INCLUDE_PATH
to the include chain (/usr/local/include:/usr/local/Cellar/llvm/10.0.0_3/lib/clang/10.0.0/include:/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include:/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks
)... Maybe that's the issue?
Nope!
/usr/local/include/wx-3.1/wx/stringimpl.h:66:10: fatal error: 'string' file not found
What the heck? Now it can't resolve #include <string>
? But I included the -std=c++14
flag!
I've been trying to solve this issue since yesterday, and I've posted questions in #rust-usage
and #beginners
in the Discord, but nobody answered. Eventually I got told to move my question here, since "everyone is volunteers and they probably don't want to answer your question or something" (???)
So, does anyone here know enough about this magic compiler stuff to help me out...?
macOS 10.14.6
Graveyard of things I've tried:
- Uninstalling/reinstalling developer tools, aka the
xcode-select --install
method... they don't even have anything to do with my LLVM install, so it didn't do anything - Everything on the first few pages of google
- Installing header files (as you can see it's trivial to find stdarg.h, but the compiler doesn't find it for some reason!)
- Ensuring bindgen is using the same clang (it is, it uses the exact same llvm-config as the one I invoked to find clang for my own investigation)