如何输入“货运"无需设置LD_LIBRARY_PATH shell变量? [英] How can I type "cargo run" without needing to set the LD_LIBRARY_PATH shell variable?

查看:61
本文介绍了如何输入“货运"无需设置LD_LIBRARY_PATH shell变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个Rust程序,该程序通过C接口调用C ++函数.为了执行程序,我必须运行:

I build a Rust program that calls a C++ function via a C interface. In order to execute the program, I have to run:

export LD_LIBRARY_PATH=<path to shared c lib>

或者我得到一个错误:

error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory

我尝试使用 std :: process :: Command

Command::new("sh").arg("export").arg("LD_LIBRARY_PATH=<path to shared c lib>");

尽管命令执行没有错误,但未设置变量.在执行程序时如何从程序中设置变量?

Although the command executes without an error, the variable is not set. How can I set the variable from my program while it is being executed?

更具体地说,我只想输入:

To be more concrete, I want to type only this:

cargo run

代替

export LD_LIBRARY_PATH=<path to shared c lib>
cargo run

到目前为止,我的代码:

My code so far:

main.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 extern crate c_interface;
pub extern crate libc;
use libc::{c_int};



#[link(name = "test")]
extern "C" {
    fn hello_world () -> c_int;
}


fn main() {
    let x;
    unsafe {
        x = hello_world();
    }
    println!("x is: {}", x);
}

test.c

#include "foo.h"

int hello_world () {
    int a = foo();
    return a;
}

foo.cpp

#include <iostream>
#include "foo.h"

using namespace std;

int foo() {
    cout << "Hello, World!" << endl;
    return 0;
}

foo.h

#ifdef __cplusplus
extern "C" {
#endif

int foo();

#ifdef __cplusplus
}
#endif

build.rs

fn main () {
    println!(r"cargo:rustc-link-search=native=/home/jan/Uni/Bachelorarbeit/Programme/Rust_Cpp_crossover_erneut");
}

我看过如何在锈?,但这不是解决我问题的方法.

I have seen How do I specify the linker path in Rust? and it is not a solution to my problem.

推荐答案

将以下行添加到build.rs:

Add the following line to build.rs:

println!("cargo:rustc-env=LD_LIBRARY_PATH=/home/jan/Uni/Bachelorarbeit/Programme/Rust_Cpp_crossover_erneut/");

这仅适用于 cargo run ,不适用于 cargo build ,然后执行输出.环境变量不会保存到二进制文件中.

This only works with cargo run and not with cargo build and then executing the output. The environment variable does not get saved into the binary.

这篇关于如何输入“货运"无需设置LD_LIBRARY_PATH shell变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆