ld:从 Rust 调用 go 函数时找不到库

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

我想通过一个共享的 c 库从 Rust 调用一个 go 函数,但是遇到了一个库链接器问题。

这是我的代码:

  1. 你好.go
package main

import "fmt"
import "C"

//export Hello
func Hello() {
    fmt.Println("Hello Go C-Shared")
}

func main() {}

go build -o Hello.so -buildmode=c-shared hello.go 

输出

Hello.h
文件和
Hello.so
文件。

  1. 接下来我创建了一个rust-hello-world项目,将
    Hello.h
    Hello.so
    文件移动到main.rs的同一目录
    src
    ,并在main.rs中调用外部Hello函数
#[link(name = "Hello")]
extern "C" {
    fn Hello();
}

fn main() {
    unsafe {Hello()};
}
  1. 我做了一些研究并尝试了以下环境变量
    cargo build
  • LD_LIBRARY_PATH=~/dev/rust-hello-world/src/
  • LD_LIBRARY_PATH=~/dev/rust-hello-world/src/Hello.so
  • export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/dev/rust-hello-world/src/
  • export LIBRARY_PATH=$LIBRARY_PATH:~/dev/rust-hello-world/src/ 

但是他们都产生了同样的错误

error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="..."
  = note: ld: library not found for -lHello
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

版本信息:

cargo 1.69.0-nightly (9d5b32f50 2023-02-22)

go version go1.19 darwin/amd64

os version: MacOS Ventura 13.1 (22C65)

如果您需要更多详细信息,请告诉我。

c go rust linker shared-libraries
© www.soinside.com 2019 - 2024. All rights reserved.