环境
$ cargo --version
cargo 1.41.0 (626f0f40e 2019-12-03)
$ rustc --version
rustc 1.41.0 (5e1a79984 2020-01-27)我所做的
$ cargo new xxx
Created binary (application) `xxx` package我编辑了Cargo.toml如下所示:
[package]
name = "xxx"
version = "0.1.0"
authors = ["Martin Marconcini <xxx@xxx.com>"]
edition = "2018"
[dependencies]
git2 = "0.12.0"和main.rs
use git2::Repository;
fn main() {
println!("Hello, world!");
let repo = match Repository::open("fake/path") {
Ok(repo) => repo,
Err(e) => panic!("Failed to init {}", e),
};
}问题
我知道fake/path不能工作,但我希望程序在运行时会构建、链接和恐慌。相反,当我做货物运输时,我得到的是:
$ cargo run
Updating crates.io index
Compiling libc v0.2.67
Compiling pkg-config v0.3.17
Compiling autocfg v1.0.0
Compiling smallvec v1.2.0
Compiling matches v0.1.8
Compiling bitflags v1.2.1
Compiling log v0.4.8
Compiling cfg-if v0.1.10
Compiling percent-encoding v2.1.0
Compiling openssl-probe v0.1.2
Compiling unicode-bidi v0.3.4
Compiling unicode-normalization v0.1.12
Compiling jobserver v0.1.21
Compiling idna v0.2.0
Compiling cc v1.0.50
Compiling url v2.1.1
Compiling libz-sys v1.0.25
Compiling openssl-sys v0.9.54
Compiling libssh2-sys v0.2.16
Compiling libgit2-sys v0.11.0+0.99.0
Compiling git2 v0.12.0
Compiling xxx v0.1.0 (/home/martin/dev/rust/xxx)
warning: unused variable: `repo`
--> src/main.rs:6:9
|
6 | let repo = match Repository::open("/fake/path") {
| ^^^^ help: consider prefixing with an underscore: `_repo`
|
= note: `#[warn(unused_variables)]` on by default
error: linking with `cc` failed: exit code: 1
|
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释
* = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620.1g3apxivazecmby1.rcgu.o" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620.1ktaa7mhzl12qb8s.rcgu.o" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620.1of15nz9rghhwcp.rcgu.o" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620.1pzhxyvu6x3rpyd4.rcgu.o" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620.1xjrgwir8ul0gxyn.rcgu.o" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620.236nnnkop2p1q7o.rcgu.o" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620.346upd227a1u5o1x.rcgu.o" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620.4vpli3o8gp9bo7bb.rcgu.o" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620.fjvxd6ccfznqzmo.rcgu.o" "-o" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620" "/home/martin/dev/rust/xxx/target/debug/deps/xxx-fe6ac189d82a9620.2bpv5j881nolkdhg.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/martin/dev/rust/xxx/target/debug/deps" "-L" "/home/martin/dev/rust/xxx/target/debug/build/libgit2-sys-9a380c1f1af1ff52/out/build" "-L" "/usr/lib/x86_64-linux-gnu" "-L" "/home/martin/dev/rust/xxx/target/debug/build/libssh2-sys-76c9f6db4b487643/out/build" "-L" "/usr/lib/x86_64-linux-gnu" "-L" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/martin/dev/rust/xxx/target/debug/deps/libgit2-e4070b896a12b23e.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/libopenssl_probe-02ad0051245c142f.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/liburl-752d8a16592303c8.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/libpercent_encoding-3f375297ed36a953.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/libidna-a849b89d54cc2589.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/libunicode_normalization-81788f9135a21d94.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/libsmallvec-e36b82e338fe18d3.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/libunicode_bidi-901a064d2af6777d.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/libmatches-ccee7833d11c4363.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/liblog-aba47d2dbe489265.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/libcfg_if-fd0f59c38961bb3a.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/liblibgit2_sys-b077f325e2a7224c.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/liblibssh2_sys-187467b0efb35c13.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/libopenssl_sys-b21cd1cefc781387.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/liblibz_sys-c72b1cae7b178c73.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/liblibc-a68ece9bb60c0ea2.rlib" "/home/martin/dev/rust/xxx/target/debug/deps/libbitflags-468acf5fe2f3e68a.rlib" "-Wl,--start-group" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-9d59e25e2eb2384d.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-4affccee0db9d60e.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-cd6b495608e342d1.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-347d88a586b28b4c.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace-46f3dc0b263b900a.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libbacktrace_sys-6789aa6fbcfdf14c.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-9f9436f1ab051941.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-ad8ec269e6d86d01.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-3699f95d33475c27.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-fe798441633702e1.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-979288990efe6065.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-2181f1e1dd7399d7.rlib" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-b8952fd765001238.rlib" "-Wl,--end-group" "/home/martin/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-e99fedbd2b621435.rlib" "-Wl,-Bdynamic" "-lz" "-lssl" "-lcrypto" "-lz" "-lutil" "-ldl" "-lutil" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-lutil"
*/
= note: /usr/bin/ld: /home/martin/dev/rust/xxx/target/debug/deps/liblibssh2_sys-187467b0efb35c13.rlib(libgcrypt.o): in function `_libssh2_rsa_new':
(.text+0x48): undefined reference to `gcry_sexp_build'
/usr/bin/ld: (.text+0x77): undefined reference to `gcry_sexp_build'
/usr/bin/ld: /home/martin/dev/rust/xxx/target/debug/deps/liblibssh2_sys-187467b0efb35c13.rlib(libgcrypt.o): in function `_libssh2_rsa_sha1_verify':
(.text+0xcb): undefined reference to `gcry_md_hash_buffer'
/usr/bin/ld: (.text+0xe8): undefined reference to `gcry_sexp_build'
/usr/bin/ld: (.text+0x103): undefined reference to `gcry_sexp_build'
/usr/bin/ld: (.text+0x118): undefined reference to `gcry_pk_verify'
/usr/bin/ld: (.text+0x123): undefined reference to `gcry_sexp_release'
/usr/bin/ld: (.text+0x12d): undefined reference to `gcry_sexp_release'
/usr/bin/ld: (.text+0x166): undefined reference to `gcry_sexp_release'
/usr/bin/ld: /home/martin/dev/rust/xxx/target/debug/deps/liblibssh2_sys-187467b0efb35c13.rlib(libgcrypt.o): in function `_libssh2_dsa_new':
(.text+0x1cc): undefined reference to `gcry_sexp_build'
/usr/bin/ld: (.text+0x203): undefined reference to `gcry_sexp_build'
/usr/bin/ld: /home/martin/dev/rust/xxx/target/debug/deps/liblibssh2_sys-187467b0efb35c13.rlib(libgcrypt.o): in function `_libssh2_rsa_sha1_sign':
(.text+0x73f): undefined reference to `gcry_sexp_build'
/usr/bin/ld: (.text+0x757): undefined reference to `gcry_pk_sign'
/usr/bin/ld: (.text+0x763): undefined reference to `gcry_sexp_release'
/usr/bin/ld: (.text+0x77d): undefined reference to `gcry_sexp_find_token'
/usr/bin/ld: (.text+0x79d): undefined reference to `gcry_sexp_nth_data'
/usr/bin/ld: /home/martin/dev/rust/xxx/target/debug/deps/liblibssh2_sys-187467b0efb35c13.rlib(libgcrypt.o): in function `_libssh2_dsa_sha1_sign':
(.text+0x87c): undefined reference to `gcry_sexp_build'
/usr/bin/ld: (.text+0x896): undefined reference to `gcry_pk_sign'
/usr/bin/ld: (.text+0x8a2): undefined reference to `gcry_sexp_release'
/usr/bin/ld: (.text+0x8d0): undefined reference to `gcry_sexp_find_token'
/usr/bin/ld: (.text+0x8f3): undefined reference to `gcry_sexp_nth_data'
/usr/bin/ld: (.text+0x937): undefined reference to `gcry_sexp_release'
/usr/bin/ld: (.text+0x946): undefined reference to `gcry_sexp_release'
/usr/bin/ld: (.text+0x985): undefined reference to `gcry_sexp_release'
/usr/bin/ld: (.text+0x998): undefined reference to `gcry_sexp_find_token'
/usr/bin/ld: (.text+0x9b2): undefined reference to `gcry_sexp_nth_data'
/usr/bin/ld: /home/martin/dev/rust/xxx/target/debug/deps/liblibssh2_sys-187467b0efb35c13.rlib(libgcrypt.o): in function `_libssh2_dsa_sha1_verify':
(.text+0xa62): undefined reference to `gcry_md_hash_buffer'
/usr/bin/ld: (.text+0xa84): undefined reference to `gcry_sexp_build'
/usr/bin/ld: (.text+0xab2): undefined reference to `gcry_sexp_build'
/usr/bin/ld: (.text+0xac9): undefined reference to `gcry_pk_verify'
/usr/bin/ld: (.text+0xad4): undefined reference to `gcry_sexp_release'
/usr/bin/ld: (.text+0xade): undefined reference to `gcry_sexp_release'
/usr/bin/ld: (.text+0xb16): undefined reference to `gcry_sexp_release'
/usr/bin/ld: /home/martin/dev/rust/xxx/target/debug/deps/liblibssh2_sys-187467b0efb35c13.rlib(libgcrypt.o): in function `_libssh2_cipher_init':
(.text+0xb5a): undefined reference to `gcry_cipher_get_algo_keylen'
/usr/bin/ld: (.text+0xb6c): undefined reference to `gcry_cipher_open'
/usr/bin/ld: (.text+0xb7f): undefined reference to `gcry_cipher_setkey'
/usr/bin/ld: (.text+0xba4): undefined reference to `gcry_cipher_get_algo_blklen'
/usr/bin/ld: (.text+0xbb8): undefined reference to `gcry_cipher_setiv'
/usr/bin/ld: (.text+0xbc5): undefined reference to `gcry_cipher_close'
/usr/bin/ld: (.text+0xbd1): undefined reference to `gcry_cipher_setctr'
/usr/bin/ld: /home/martin/dev/rust/xxx/target/debug/deps/liblibssh2_sys-187467b0efb35c13.rlib(libgcrypt.o): in function `_libssh2_cipher_crypt':
(.text+0xc02): undefined reference to `gcry_cipher_encrypt'
/usr/bin/ld: (.text+0xc11): undefined reference to `gcry_cipher_decrypt'
/usr/bin/ld: /home/martin/dev/rust/xxx/target/debug/deps/liblibssh2_sys-187467b0efb35c13.rlib(global.o): in function `libssh2_init':
(.text+0x38): undefined reference to `gcry_control'
collect2: error: ld returned 1 exit status-dev依赖项或什么的。cargo build编译了它,它起了作用。
接下来我要去哪里?我的猜测是我错过了一个库,或者我拥有的版本不一样。
发布于 2020-03-11 18:22:34
我想通了(经过一点试验和运气)。
整个问题与ld和git使用SSH连接所需的library库的链接过程类似。
Pop!_OS以包含了许多“开发所需”的东西而闻名,所以我肯定我拥有了所有的东西(我做了)。
我怎么修好的?
我查看了/etc/ld.so.conf.d/目录。有一些".conf“文件在那里。x86_64-linux-gnu.conf指着/usr/lib/x86_64-linux-gnu,所以我去那里看哪个库与libssh*匹配。
我发现了这个:
$ ls -ls /usr/lib/x86_64-linux-gnu/libssh*
312 -rw-r--r-- 1 root root 316732 Sep 5 2019 libssh2.a
0 lrwxrwxrwx 1 root root 16 Sep 5 2019 libssh2.so -> libssh2.so.1.0.1
0 lrwxrwxrwx 1 root root 16 Sep 5 2019 libssh2.so.1 -> libssh2.so.1.0.1
184 -rw-r--r-- 1 root root 186856 Sep 5 2019 libssh2.so.1.0.1
1040 -rw-r--r-- 1 root root 1061468 Dec 10 16:24 libssh.a
0 lrwxrwxrwx 1 root root 22 Dec 10 16:24 libssh-gcrypt.so.4 -> libssh-gcrypt.so.4.8.1
552 -rw-r--r-- 1 root root 564760 Dec 10 16:24 libssh-gcrypt.so.4.8.1
0 lrwxrwxrwx 1 root root 11 Dec 10 16:24 libssh.so -> libssh.so.4
0 lrwxrwxrwx 1 root root 15 Dec 10 16:24 libssh.so.4 -> libssh.so.4.8.1
552 -rw-r--r-- 1 root root 564856 Dec 10 16:24 libssh.so.4.8.1当将此输出与新安装的VM (而不是非)进行比较时,我注意到VM中并不存在所有的libssh2*文件。
由于链接器错误都是高声的(我认为是cryPT的意思),所以我决定尝试删除这两个版本,只留下一个可以工作的版本:
$ sudo apt remove libssh2-1-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libhttp-parser-dev libmbedtls-dev
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
libgit2-dev libssh2-1-dev
0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
After this operation, 3,700 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 412520 files and directories currently installed.)
Removing libgit2-dev:amd64 (0.27.7+dfsg.1-0.2build1) ...
Removing libssh2-1-dev:amd64 (1.8.0-2.1build1) ...
Processing triggers for man-db (2.8.7-3) ...在这样做之后,我得到了预期的输出:
$ cargo run
warning: unused variable: `repo`
--> src/main.rs:6:9
|
6 | let repo = match Repository::open("fakerepo") {
| ^^^^ help: consider prefixing with an underscore: `_repo`
|
= note: `#[warn(unused_variables)]` on by default
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/xxx`
Hello, world!
thread 'main' panicked at 'Failed to init failed to resolve path 'fakerepo': No such file or directory; class=Os (2); code=NotFound (-3)', src/main.rs:8:19
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.如果我初始化一个git并再次运行它,就不会像预期的那样出现错误:
$ mkdir fakerepo; cd fakerepo ; git init ; cd ..
Initialized empty Git repository in /home/martin/dev/rust/xxx/fakerepo/.git/
$ cargo run
warning: unused variable: `repo`
--> src/main.rs:6:9
|
6 | let repo = match Repository::open("fakerepo") {
| ^^^^ help: consider prefixing with an underscore: `_repo`
|
= note: `#[warn(unused_variables)]` on by default
Finished dev [unoptimized + debuginfo] target(s) in 0.03s
Running `target/debug/xxx`
Hello, world!看来问题是图书馆。我将进一步研究它从哪里来(谁安装了它),更重要的是,如何使它成为一个非问题(必须有一个方法来告诉这个整个构建过程的库选择),但这是另一个问题的另一天。
我做了什么才发现的?
VM技巧,是我以前为解决其他问题所做的事情。Linux在VM上安装非常容易/快速,Gnome盒已经安装并配置在我的Pop!_OS上,所以我花了1分钟才启动它,并在我面前摆了一个新安装的操作系统(好吧,它不是新鲜的,我不得不在VM中下载150个更新):
无论如何,我有一个薄荷OS安装;一个超级轻Ubuntu的发行版,没有太多的阻碍,所以它是完美的我的需要。在Peppermint上,我需要做的就是开始:
sudo apt install pkg-configsudo apt install libssl-devsudo apt install curl (是的,它不带卷发)curl https://sh.rustup.rs -sSf | sh)在这样做之后,我能够运行相同的hello world项目。当我意识到它正在运行这个VM时,我又在一个流行的!_OS 19.10刚刚安装的VM上做了一次。再说一次,成功了。(这让我大吃一惊,因为我错误地指责"Pop_OS")。
事实上,这两个VM的工作,几乎没有从我的输入,促使我继续尝试我的主机;我很高兴我做到了。
https://stackoverflow.com/questions/60620025
复制相似问题