Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error "'stdlib.h' file not found" when running jextract on the C binding for a Rust project

I want to use Project Panama's jextract tool to build a Java binding to a Rust library. When running the following command, I get an error:

jextract -C -x -C c++ -I /Library/Developer/CommandLineTools/usr/include/c++/v1 -t adder -o adder.jar bindings.h
java.lang.RuntimeException: /Library/Developer/CommandLineTools/usr/include/c++/v1/stdlib.h:93:15: fatal error: 'stdlib.h' file not found

I'm confused because the include path contains stdlib.h:

ls /Library/Developer/CommandLineTools/usr/include/c++/v1/ | grep stdlib                                        
cstdlib
stdlib.h

The line in error contains only #include_next <stdlib.h>.

My Rust source is a simple function:

#[no_mangle]
pub extern "C" fn addition(a: u32, b: u32) -> u32 {
    a + b
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn adds() {
        assert_eq!(addition(1, 2), 3);
    }
}

The bindings.h header is generated by the cbindgen crate:

#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>

extern "C" {

uint32_t addition(uint32_t a, uint32_t b);

} // extern "C"

What do I need to do for jextract to locate stdlib.h?

like image 833
amb85 Avatar asked Oct 22 '25 19:10

amb85


1 Answers

This was a case of missing an include path - I needed to include the MacOS SDK stdlib.h header file location also. This wasn't clear from the error.

The correct command to have run was:

jextract -C -x -C c++ -I /Library/Developer/CommandLineTools/usr/include/c++/v1 -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -t adder -o adder.jar bindings.h
like image 183
amb85 Avatar answered Oct 24 '25 10:10

amb85



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!