Linker error: undefined reference to `rust::cxxbridge1::String::~String()'
Hi, i don't have much experience with C++(about 6 months of rust though), so i think i'm missing something obvious, but i'm not sure what exactly.
I want to call rust's function 'hello' from C++. I'm following the book, specifically "Extern Rust" section.
$ (projects/rcnet) cargo build --target-dir /tmp/rcnet
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
$ (projects/cpp) g++ -o hello helloworld.cpp
/usr/bin/ld: /tmp/ccK3CZsY.o: in function `main':
helloworld.cpp:(.text+0x24): undefined reference to `hello()'
/usr/bin/ld: helloworld.cpp:(.text+0x37): undefined reference to `rust::cxxbridge1::operator<<(std::ostream&, rust::cxxbridge1::String const&)'
/usr/bin/ld: helloworld.cpp:(.text+0x43): undefined reference to `rust::cxxbridge1::String::~String()'
/usr/bin/ld: helloworld.cpp:(.text+0x6c): undefined reference to `rust::cxxbridge1::String::~String()'
collect2: error: ld returned 1 exit status
// projects/cpp/helloworld.cpp
#include <iostream>
#include "/tmp/rcnet/cxxbridge/rust/cxx.h"
#include "/tmp/rcnet/cxxbridge/rcxx/src/main.rs.h"
int main()
{
std::cout << hello();
}
// projects/rcnet/build.rs
fn main() {
cxx_build::bridge("src/main.rs")
.compile("rust-net");
}
// projects/rcnet/scr/main.rs
#[cxx::bridge]
mod ffi {
struct Player {
pos: u32,
}
extern "Rust" {
fn hello() -> String;
}
}
pub fn hello() -> String {
"hello!".to_string()
}
fn main() {
println!("Hello, world!");
}
// /tmp/rcnet/cxxbridge/rcxx/src/main.rs.h
// ... last relevant(imo) lines, can include more, it's 79 lines long
#endif // CXXBRIDGE1_RUST_STRING
} // namespace cxxbridge1
} // namespace rust
struct Player;
#ifndef CXXBRIDGE1_STRUCT_Player
#define CXXBRIDGE1_STRUCT_Player
struct Player final {
::std::uint32_t pos;
using IsRelocatable = ::std::true_type;
};
#endif // CXXBRIDGE1_STRUCT_Player
::rust::String hello() noexcept;
main.rs.h looks sane to my eyes, i guess?
I've tried searching web, most relevant link i think i got was this stackoverflow discussion on linker error, but i'm not really sure what to make of it.
please help?
so a colleague of mine suggested that i need to link a compiled rust code. i'm not experienced in this, and my initial take didn't work, unfortunately. but i feel like it's the right idea, though :)
/tmp/rcnet
$ fd '\.a'
debug/build/cxx-4a230aa16201c7ee/out/libcxxbridge1.a
debug/build/link-cplusplus-9dccbd69626f1a80/out/liblink-cplusplus.a
debug/build/rcxx-d53c10d40e6078b8/out/librust-net.a
$ g++ -L /tmp/rcnet/debug/build/rcxx-d53c10d40e6078b8/out -L /tmp/rcnet/debug/build/cxx-4a230aa16201c7ee/out/ -L /tmp/rcnet/debug/build/link-cplusplus-9dccbd69626f1a80/out -l:librust-net.a -l:libcxxbridge1.a -l:liblink-cplusplus.a -o hello helloworld.cpp
/usr/bin/ld: /tmp/ccb4ygkZ.o: in function `main':
helloworld.cpp:(.text+0x24): undefined reference to `hello()'
/usr/bin/ld: helloworld.cpp:(.text+0x37): undefined reference to `rust::cxxbridge1::operator<<(std::ostream&, rust::cxxbridge1::String const&)'
/usr/bin/ld: helloworld.cpp:(.text+0x43): undefined reference to `rust::cxxbridge1::String::~String()'
/usr/bin/ld: helloworld.cpp:(.text+0x6c): undefined reference to `rust::cxxbridge1::String::~String()'
collect2: error: ld returned 1 exit status
You need to link to your library when compiling c++ code.
While rust::String is implemented in cxx.cc. You would also need to add it to the source list:
g++ -o hello helloworld.cpp cxx.cc -L ./target/debug -lrcnet
While
rust::Stringis implemented in cxx.cc. You would also need to add it to the source list:
Where is this cxx.cc ? I did not find it in under targer/
I just have
let _ = cxx_build::bridge("src/lib.rs");
in my build.rs
@tonky String::~String() is defined in C++ here and compiled here into libcxxbridge1.a.
$ cargo build
$ nm target/debug/build/cxx-4a230aa16201c7ee/out/libcxxbridge1.a | c++filt | rg String::~String
T rust::cxxbridge1::String::~String()
You would need to make sure libcxxbridge1.a is available to the linker during linking the C++ main.
If you are linking via GNU ld or gold (likely yes if you are invoking the linker through g++) then there is an extra complexity that the link line is order sensitive. Read https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc. In brief, for every symbol, the library that needs the symbol must appear first, then the library that resolves the symbol. So if helloworld.cpp needs the symbol and libcxxbridge1.a contains it, they must be in that order in the linker invocation. I notice you have them in the wrong order in https://github.com/dtolnay/cxx/issues/875#issuecomment-839944555 which is why the link would have failed.
If you are linking with lld instead, it's easier because there is no order sensitivity. It resolves the dependency graph in a better way.
@dtolnay ,
You would need to make sure libcxxbridge1.a is available to the linker during linking the C++
Currently it is quite problematic to find libcxxbridge1.a. AFAIK there is no way to override OUT_DIR for the specific dependency or to get the right directory name (cxx-2878606df310452f). Maybe it would make sense to copy libcxxbridge1.a to the /target/cxxbridge/ directory?