我想通过一个共享的 c 库从 Rust 调用一个 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
文件。
Hello.h
和Hello.so
文件移动到main.rs的同一目录src
,并在main.rs中调用外部Hello函数#[link(name = "Hello")]
extern "C" {
fn Hello();
}
fn main() {
unsafe {Hello()};
}
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)
如果您需要更多详细信息,请告诉我。