Segfault with rust, musl and c++


I have the following setup. In src/

extern "C" {

void *test() {
    return new int();


and in src/

use std::os::raw::c_void;

extern "C" {
    #[link_name = "test"]
    fn test() -> *const c_void;

fn main() {
    println!("{:?}", unsafe { test() });


use std::path::Path;

fn main() {
    let dir = Path::new("src");


cargo run works fine with x86_64-unknown-linux-gnu but when I try running with x86_64-unknown-linux-musl I get:

Segmentation fault (core dumped)

I think the error is something to do with rust and musl-g++ disagreeing about which c++ standard library to use. gbd confirms that the segfault happens in the call to new int();. Are there any pointers as to what is going wrong here? I am using unsafe to call the external function but I don't think I am doing anything to deserve a segfault.

SSCCE here: (I am using a musl toolchain compiled from because arch linux don't have a prebuilt musl c++ that I can install).

1 Like

This is a minimised example though! For the real project I want to compile I do need to (ideally statically) link in libstdc++. My program compiles, links and runs fine with the (default) x86_64-unknown-linux-gnu target. With x86_64-unknown-linux-musl is compiles and links fine but segfaults when I run it, I am looking to understand why and how to fix it!