我不知道如何将C库链接到Rust.这就是我所做的:
我的lib.rs文件包含
#[link(name = "test")]
extern {
Run Code Online (Sandbox Code Playgroud)
该库已建立并具有名称libtest.a.
我不知道该把它放在哪里.我已经尝试了几个地方,但在做这个时我仍然有这种类型的错误cargo run
error: linking with `cc` failed: exit code: 1
//..
note: /usr/bin/ld: no se puede encontrar -ltest
note: /usr/bin/ld: no se puede encontrar -ltest
note: /usr/bin/ld:.......
//..
Run Code Online (Sandbox Code Playgroud)
上述翻译/usr/bin/ld: no se puede encontrar -ltest- >usr/bin/ld: cannot find -ltest
我不知道往哪里放libtest.a,这样/usr/bin/ld可以找到它.Cargo没有告诉我图书馆应该在项目中的哪个位置.
我的Cargo.toml包含
[dependencies.test]
path = "./src/test"
[dependencies]
bitflags = "0.7"
libc = "0.2"
[build-dependencies]
make-cmd = "0.1"
Run Code Online (Sandbox Code Playgroud)
在再次阅读文档的FFI部分之后,我认为可能之前的错误消息是因为我在寻找共享库,所以我做了以下更改: …
我正在尝试将Rust程序与libsoundio链接起来.我正在使用Windows,并且可以使用GCC二进制下载.如果我把它放在与我的项目相同的文件夹中,我可以像这样链接它:
#[link(name = ":libsoundio-1.1.0/i686/libsoundio.a")]
#[link(name = "ole32")]
extern {
fn soundio_version_string() -> *const c_char;
}
Run Code Online (Sandbox Code Playgroud)
但我真的想指定#[link(name = "libsoundio")]甚至#[link(name = "soundio")],然后在其他地方提供链接器路径.
我在哪里可以指定该路径?
我尝试了rustc-link-search如下建议:
#[link(name = "libsoundio")]
#[link(name = "ole32")]
extern {
fn soundio_version_string() -> *const c_char;
}
Run Code Online (Sandbox Code Playgroud)
并在.cargo/config:
[target.i686-pc-windows-gnu.libsoundio]
rustc-link-search = ["libsoundio-1.1.0/i686"]
rustc-link-lib = ["libsoundio.a"]
[target.x86_64-pc-windows-gnu.libsoundio]
rustc-link-search = ["libsoundio-1.1.0/x86_64"]
rustc-link-lib = ["libsoundio.a"]
Run Code Online (Sandbox Code Playgroud)
但它仍然只传递"-l" "libsoundio"给gcc并且失败了ld: cannot find -llibsoundio.我错过了一些非常明显的东西吗 文档似乎表明这应该有效.
我构建了一个 Rust 程序,通过 C 接口调用 C++ 函数。为了执行该程序,我必须运行:
export LD_LIBRARY_PATH=<path to shared c lib>
Run Code Online (Sandbox Code Playgroud)
或者我收到错误:
error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下命令在构建脚本中设置变量std::process::Command
Command::new("sh").arg("export").arg("LD_LIBRARY_PATH=<path to shared c lib>");
Run Code Online (Sandbox Code Playgroud)
尽管该命令执行时没有错误,但该变量未设置。如何在程序执行时设置该变量?
更具体地说,我只想输入以下内容:
Command::new("sh").arg("export").arg("LD_LIBRARY_PATH=<path to shared c lib>");
Run Code Online (Sandbox Code Playgroud)
代替
cargo run
Run Code Online (Sandbox Code Playgroud)
到目前为止我的代码:
主程序.rs
/*---compile all with---
g++ -c -fpic foo.cpp
gcc -c -fpic test.c
g++ -shared foo.o test.o -o libtest.so
in order to execute we have to set the variable
export LD_LIBRARY_PATH=/home/jan/Uni/Bachelorarbeit/Programme/Rust_Cpp_crossover_erneut/$LD_LIBARY_PATH
*/
//pub …Run Code Online (Sandbox Code Playgroud) 我有一个共享库,我想动态链接到几个单独的二进制货物应用程序.我使用-- -L /path/to/dir格式在链接器中包含它的位置,并且应用程序正确编译,我期望的二进制大小显着减少.但是,在使用时检查生成的二进制文件时ldd,我收到一条消息,指出无法找到该库:
casey@Gilthar-II:~/bot4/backtester/target/release$ ldd backtester
linux-vdso.so.1 => (0x00007ffc642f7000)
libalgobot_util.so => not found
Run Code Online (Sandbox Code Playgroud)
如果我将库添加到/lib/x86_64-linux-gnu目录,应用程序运行没有问题.
有没有办法让Rust在与二进制文件相同的目录中查找.so文件,或者在二进制文件目录中的lib目录中查找要在运行时加载的文件?如果那是不可能的,有没有办法至少让Rust插入它所链接的库的绝对路径?
我试过设置rpath = true没有效果.