dtolnay / cxx

Safe interop between Rust and C++
https://cxx.rs
Apache License 2.0
5.82k stars 330 forks source link

Undefined symbols for architecture arm64: "rust::cxxbridge1::Str::Str(char const*)" #1271

Closed Ghamza-Jd closed 1 year ago

Ghamza-Jd commented 1 year ago

Hey 👋🏻

I'm trying to pass a string from C++ to Rust, but whenever I try to pass that, I got:

Undefined symbols for architecture arm64:
  "rust::cxxbridge1::Str::Str(char const*)", referenced from:
      _main in main-3668b6.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Now, here's a minimal example of what I'm doing:

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        fn greet(name: &str);
    }
}

pub fn greet(name: &str) {
    println!("Hello {name}");
}
#include <iostream>
#include "logger.rs.h"

using namespace std;

int main()
{
    greet("world");
    return 0;
}
bin_name = app
lib_path = ../target/debug/
lib_name = world
bindings_path = ../target/cxxbridge/cpp/src/

.PHONY: all

all: compile run

compile:
    @cargo build
    @mkdir -p build
    @g++ -std=c++17 \
        -o build/$(bin_name) \
        $(bindings_path)/*.cc *.cc \
        -I $(bindings_path) \
        -L $(lib_path) \
        -l $(lib_name)

run:
    @./build/$(bin_name)

clean:
    @rm -rf ./build

I've tried passing creating an opaque type consist of primitive types like u32, it worked and I was able to call a function with a reference to the opaque type.

But when dealing with &str and String the mentioned compiler error appears.

Thanks!

Ghamza-Jd commented 1 year ago

When I looked into the generated .cc file, I found implementation for the Box but for the Str I could only find interface definition

Ghamza-Jd commented 1 year ago

It worked now using CxxString, but I'm curious why both &str and String aren't working

Ghamza-Jd commented 1 year ago

The crate type was crate-type = ["cdylib", "staticlib"], I've removed the cdylib and the types that weren't working are now working. So now I can use Rust, &str, and String

shenjiangqiu commented 4 months ago

same issue, and I what to know why?