Linking simple .cpp file on build.rs gives undefined reference to operator new and other C++ things

I have this very small Rust project which should compile a cpp file and link against it: GitHub - lucaszanella/cpp_calls_rust_back at 4e436209088cc3d5e90e8a6d93a3c0f3d6081d54 but I'm getting undefined references to things like operator new:

root@7ca36544f4d5:/workspaces/rust-cpp-interface/cpp_calls_rust_back# cargo build
   Compiling cpp_calls_rust_back v0.1.0 (/workspaces/rust-cpp-interface/cpp_calls_rust_back)
warning: src/openvpnclient.cpp: In function 'int openvpn_client_run(OpenVpnClient*)':
warning: src/openvpnclient.cpp:47:9: warning: unused variable 'r' [-Wunused-variable]
warning:      int r = client->callbacks.on_read(b, 5, client->callbacks.user_data);
warning:          ^
warning: src/openvpnclient.cpp:47:72: warning: 'b' is used uninitialized in this function [-Wuninitialized]
warning:      int r = client->callbacks.on_read(b, 5, client->callbacks.user_data);
warning:                                                                         ^
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-Wl,--eh-frame-hdr" "-L" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.157iqs1242fa2khb.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.168cs4eklmbumqbi.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.18cmvqhlw07z7a14.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.1n6vkob83pqg0pdo.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.1t1dnjrwk3gsr4ph.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.1z6c9am4eb6ybt37.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.1znp3euuflel734x.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.1ztvtrlbw29ugz8i.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.21uzur0a14dn5t4d.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.22n757332z5eedm7.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.24nwjueph60lou1v.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.2w4uhrw0kk3lt4g9.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.37mfwn22tf7j8ibv.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.3f8knjd7r4ni8sg8.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.3faklgu9iy0880nk.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.3l8q0vadi94xg15t.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.42aj3zdpzfiy4vqs.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.45fcis7kq5wu75mb.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.4ba4ya1p430kfakh.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.4dh38bgg7bhzbdce.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.4hihi0hodu6s4v00.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.4pi0v3123wil4qcd.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.4ucsi4tb9rao4x69.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.57vnlqx6zx6o42x5.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.ngl118iukscc28.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.noiz47qlxnphy7v.rcgu.o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.wkce7boxmf8dqaa.rcgu.o" "-o" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps/cpp_calls_rust_back-0d2b17987cc1b786.1vi3fttc8mtzxc9r.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/deps" "-L" "/workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/build/cpp_calls_rust_back-a18d013923294fda/out" "-L" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "-Wl,--whole-archive" "-lopenvpnclient" "-Wl,--no-whole-archive" "-Wl,--start-group" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-e12de7683a34c500.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-446a53c2e468675f.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-86fef9fb252a2d33.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-3bed9bfd30304751.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-0e38eeb474a7b386.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-a846ed547b824740.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-c9540f0a4fb9265d.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-7d2ca66c92c356eb.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-147e2c808f949493.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-bd2a4369506670e9.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-74bfdd0ce7d0ed48.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-6777f47f1c491841.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-b104f1790174f2be.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-db37898996f45c13.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-25b7435131fc4938.rlib" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-9d4b6f3c097e876e.rlib" "-Wl,--end-group" "/root/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-3f953ad652ab829a.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc"
  = note: /workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/build/cpp_calls_rust_back-a18d013923294fda/out/libopenvpnclient.a(openvpnclient.o): In function `openvpn_client_new':
          /workspaces/rust-cpp-interface/cpp_calls_rust_back/src/openvpnclient.cpp:39: undefined reference to `operator new(unsigned long)'
          /workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/build/cpp_calls_rust_back-a18d013923294fda/out/libopenvpnclient.a(openvpnclient.o): In function `openvpn_client_run':
          /workspaces/rust-cpp-interface/cpp_calls_rust_back/src/openvpnclient.cpp:49: undefined reference to `__cxa_begin_catch'
          /workspaces/rust-cpp-interface/cpp_calls_rust_back/src/openvpnclient.cpp:49: undefined reference to `__cxa_end_catch'
          /workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/build/cpp_calls_rust_back-a18d013923294fda/out/libopenvpnclient.a(openvpnclient.o): In function `openvpn_client_free':
          /workspaces/rust-cpp-interface/cpp_calls_rust_back/src/openvpnclient.cpp:54: undefined reference to `operator delete(void*, unsigned long)'
          /workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/build/cpp_calls_rust_back-a18d013923294fda/out/libopenvpnclient.a(openvpnclient.o): In function `__static_initialization_and_destruction_0(int, int)':
          /usr/include/c++/7/iostream:74: undefined reference to `std::ios_base::Init::Init()'
          /usr/include/c++/7/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
          /workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/build/cpp_calls_rust_back-a18d013923294fda/out/libopenvpnclient.a(openvpnclient.o):(.data.rel.local.DW.ref._ZTISt9exception[DW.ref._ZTISt9exception]+0x0): undefined reference to `typeinfo for std::exception'
          /workspaces/rust-cpp-interface/cpp_calls_rust_back/target/debug/build/cpp_calls_rust_back-a18d013923294fda/out/libopenvpnclient.a(openvpnclient.o):(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error

The following warnings were emitted during compilation:

warning: src/openvpnclient.cpp: In function 'int openvpn_client_run(OpenVpnClient*)':
warning: src/openvpnclient.cpp:47:9: warning: unused variable 'r' [-Wunused-variable]
warning:      int r = client->callbacks.on_read(b, 5, client->callbacks.user_data);
warning:          ^
warning: src/openvpnclient.cpp:47:72: warning: 'b' is used uninitialized in this function [-Wuninitialized]
warning:      int r = client->callbacks.on_read(b, 5, client->callbacks.user_data);
warning:                                                                         ^

error: could not compile `cpp_calls_rust_back`

Here's my build.rs:

fn main() {
    cc::Build::new()
        .file("src/openvpnclient.cpp")
        .compile("libopenvpnclient.a");
}

I've done C++ linking successfully with the cmake crate but this example is very simple so I'd like to stick with manual compiling and linking. What is wrong?

Try adding .cpp(true) to the builder.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.