编译器未找到包含的c++文件

问题描述 投票:0回答:1

我正在使用 CXX 为 C++ 项目创建 Rust 绑定。理想情况下,我希望将绑定保留在单独的文件中,以避免直接修改原始 C++ 源代码。

作为起点,我尝试为基本的 Hello World 程序设置绑定。但是,我遇到了有关未定义符号的错误,并且我无法弄清楚如何解决此问题。我对 C++ 非常陌生,因此感谢任何帮助。

  = note: Undefined symbols for architecture x86_64:
            "hello_world()", referenced from:
                hello_world_adapter() in libcxx-hello.a[3](2e40c9e35e9506f4-hello_a.o)
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

设备信息

macOS 红杉 15.0.1 (24A348)
货物 1.82.0 (8f40fc59f 2024-08-21)

项目内容

目录结构

cxx-hello
 |- build.rs
 |- Cargo.toml
 |- include
     |- hello.h
     |- hello_a.h
 |- src
     |- hello.cc
     |- hello_a.cc
     |- ffi.rs
     |- main.rs

构建.rs

    1 fn main() {
    2     cxx_build::bridge("src/ffi.rs")
    3         .file("src/hello_a.cc")
    4         .compile("cxx-hello");
    5
    6     println!("cargo:rerun-if-changed=src/ffi.rs");
    7     println!("cargo:rerun-if-changed=src/hello.cc");
    8     println!("cargo:rerun-if-changed=src/hello_a.cc");
    9     println!("cargo:rerun-if-changed=include/hello.h");
   10     println!("cargo:rerun-if-changed=include/hello_a.h");
   11 }

货物.toml

    1 [package]
    2 name = "cxx-hello"
    3 version = "0.1.0"
    4 edition = "2021"
    5
    6 [dependencies]
    7 cxx = "1.0"
    8
    9 [build-dependencies]
   10 cxx-build = "1.0"

包含/hello.h

    1 #pragma once
    2 #include <string>
    3
    4 std::string hello_world();

包括/hello_a.h

    1 #pragma once
    2 #include "cxx-hello/include/hello.h"
    3 #include "rust/cxx.h"
    4
    5 rust::String hello_world_adapter();

src/hello.cc

    1 #include "cxx-hello/include/hello.h"
    2
    3 std::String hello_world() {
    4     return std::String("Hello World");
    5 }

src/hello_a.cc

    1 #include "cxx-hello/include/hello_a.h"
    2 #include "rust/cxx.h"
    3
    4 rust::String hello_world_adapter() {
    5     return rust::String(::hello_world());
    6 }

src/ffi.rs

    1 #[cxx::bridge]
    2 mod ffi {
    3     unsafe extern "C++" {
    4         include!("cxx-hello/include/hello_a.h");
    5
    6         fn hello_world_adapter() -> String;
    7     }
    8 }
    9
   10 pub fn hello_world() -> String {
   11     ffi::hello_world_adapter()
   12 }

src/main.rs

    1 mod ffi;
    2
    3 fn main() {
    4     let greeting = ffi::hello_world();
    5     println!("From c++: {}", greeting);
    6 }
c++ rust binding
1个回答
0
投票

您没有编译

hello.cc
。将其添加到您的 CXX 构建配置中:

cxx_build::bridge("src/ffi.rs")
    .file("src/hello_a.cc")
    .file("src/hello.cc") // <----------
    .compile("cxx-hello");
© www.soinside.com 2019 - 2024. All rights reserved.