diff options
| -rw-r--r-- | .vscode/launch.json | 52 | ||||
| -rw-r--r-- | README | 15 | ||||
| -rw-r--r-- | build.rs | 19 | ||||
| -rw-r--r-- | libsample.a | bin | 3164 -> 0 bytes | |||
| -rwxr-xr-x | libsample.so | bin | 0 -> 15520 bytes | |||
| -rw-r--r-- | sample.o | bin | 3024 -> 0 bytes |
6 files changed, 79 insertions, 7 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..cead727 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,52 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'ffi_demo'", + "cargo": { + "args": [ + "build", + "--bin=ffi_demo", + "--package=ffi_demo" + ], + "filter": { + "name": "ffi_demo", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}", + "env": { + "LD_LIBRARY_PATH": "${workspaceFolder}" + } + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'ffi_demo'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=ffi_demo", + "--package=ffi_demo" + ], + "filter": { + "name": "ffi_demo", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}", + "env": { + "LD_LIBRARY_PATH": "${workspaceFolder}" + } + } + ] +}
\ No newline at end of file @@ -1,4 +1,11 @@ -使用静态库 -- `*.a` 和 `*.o` 文件存在在同一级目录 -- build.rs 声明搜索路径: `println!("cargo:rustc-link-search=native=./");` -- 如果不想每个 extern 都写个 linkxx 可以全局声明: `println!("cargo:rustc-link-lib=static=sample");`
\ No newline at end of file +动态库就不能像 静态库那样 仅仅添加个路径就好,而是 需要修改 `LD_LIBRARY_PATH` 指向 动态库的路径; + +```bash +println!("cargo:rustc-env=LD_LIBRARY_PATH=/home/jan/Uni/Bachelorarbeit/Programme/Rust_Cpp_crossover_erneut/"); +``` + +添加如上一行, 仅仅对 cargo run 命令生效. cargo build / debug 不生效. + +总之在 cargo build/debug 前要让 LD_LIBRARY_PATH 指向 动态库的路径. + +还可以单独在 launch 的 env 下修改
\ No newline at end of file @@ -1,5 +1,18 @@ +use std::env; + fn main() { // cc::Build::new().file("sample.c").compile("sample"); - println!("cargo:rustc-link-search=native=./"); - println!("cargo:rustc-link-lib=static=sample"); -}
\ No newline at end of file + + let current_dir = env::current_dir().unwrap(); + let lib_path = current_dir.to_str().unwrap(); + env::set_var("LD_LIBRARY_PATH", lib_path); + + println!("cargo:rustc-env=LD_LIBRARY_PATH=./"); + + let current_dir = env::current_dir().unwrap(); + let lib_path = current_dir.to_str().unwrap(); + println!("cargo:rustc-link-search=native={}", lib_path); + + // println!("cargo:rustc-link-search=all=./"); + println!("cargo:rustc-link-lib=dylib=sample"); +} diff --git a/libsample.a b/libsample.a Binary files differdeleted file mode 100644 index 6cec544..0000000 --- a/libsample.a +++ /dev/null diff --git a/libsample.so b/libsample.so Binary files differnew file mode 100755 index 0000000..83f63e5 --- /dev/null +++ b/libsample.so diff --git a/sample.o b/sample.o Binary files differdeleted file mode 100644 index 4731397..0000000 --- a/sample.o +++ /dev/null |
