rust在Ubuntu上编译时解决问题的整理

博主在尝试编译Rust程序时遇到了一系列问题,包括缺少openssl库、pkg-config未安装、SSL错误以及网络连接问题。通过更新包源、安装libssl-dev和pkg-config、配置cargo国内镜像源,最终成功编译并运行程序。此过程揭示了在Linux环境中解决Rust依赖和网络问题的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我今天在编译一段rust程序的时候,遇见了几个问题,现在整理一下。
自己的 Cargo.toml如下:

name = "scrape_url"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = {
    version = "0.11",features = ["blocking"]}
html2md = "0.2"

自己的全部代码在src/main.rs如下:

use std::fs;

fn main() {
   
  let url = "https://www.baidu.com/";
  let output = "baidu.md";

  println!("获取到的地址: {}", url);
  let body = reqwest::blocking::get(url).unwrap().text().unwrap();

  println!("正在转换中");
  let md = html2md::parse_html(&body);

  fs::write(output, md.as_bytes()).unwrap();
  println!("md文件放在{}.", output);
}

使用的命令是cargo run之后全部的描述如下:

 Updating crates.io index
  Downloaded cfg-if v1.0.0
  Downloaded bytes v1.1.0
  Downloaded futures-io v0.3.21
  Downloaded hashbrown v0.11.2
  Downloaded foreign-types v0.3.2
  Downloaded url v2.2.2
  Downloaded pin-utils v0.1.0
  Downloaded rand_pcg v0.2.1
  Downloaded proc-macro2 v1.0.36
  Downloaded try-lock v0.2.3
  Downloaded tower-service v0.3.1
  Downloaded utf-8 v0.7.6
  Downloaded want v0.3.0
  Downloaded autocfg v1.1.0
  Downloaded form_urlencoded v1.0.1
  Downloaded phf v0.8.0
  Downloaded tinyvec v1.5.1
  Downloaded tracing v0.1.31
  Downloaded futures-sink v0.3.21
  Downloaded quote v1.0.15
  Downloaded pkg-config v0.3.24
  Downloaded once_cell v1.9.0
  Downloaded slab v0.4.5
  Downloaded serde v1.0.136
  Downloaded ryu v1.0.9
  Downloaded tokio-util v0.6.9
  Downloaded regex v1.5.4
  Downloaded unicode-xid v0.2.2
  Downloaded bitflags v1.3.2
  Downloaded num_cpus v1.13.1
  Downloaded parking_lot_core v0.8.5
  Downloaded smallvec v1.8.0
  Downloaded unicode-normalization v0.1.19
  Downloaded unicode-bidi v0.3.7
  Downloaded foreign-types-shared v0.1.1
  Downloaded futures-task v0.3.21
  Downloaded futures-util v0.3.21
  Downloaded xml5ever v0.16.2
  Downloaded tinyvec_macros v0.1.0
  Downloaded hyper v0.14.17
  Downloaded mac v0.1.1
  Downloaded phf_shared v0.8.0
  Downloaded mime v0.3.16
  Downloaded markup5ever_rcdom v0.1.0
  Downloaded memchr v2.4.1
  Downloaded native-tls v0.2.8
  Downloaded openssl v0.10.38
  Downloaded matches v0.1.9
  Downloaded phf_codegen v0.8.0
  Downloaded new_debug_unreachable v1.0.4
  Downloaded rand_chacha v0.2.2
  Downloaded pin-project-lite v0.2.8
  Downloaded reqwest v0.11.9
  Downloaded tokio v1.17.0
  Downloaded regex-syntax v0.6.25
  Downloaded encoding_rs v0.8.30
  Downloaded libc v0.2.118
  Downloaded ppv-lite86 v0.2.16
  Downloaded precomputed-hash v0.1.1
  Downloaded hyper-tls v0.5.0
  Downloaded httparse v1.6.0
  Downloaded parking_lot v0.11.2
  Downloaded markup5ever v0.10.1
  Downloaded ipnet v2.3.1
  Downloaded indexmap v1.8.0
  Downloaded idna v0.2.3
  Downloaded log v0.4.14
  Downloaded lock_api v0.4.6
  Downloaded httpdate v1.0.2
  Downloaded html5ever v0.25.1
  Downloaded h2 v0.3.11
  Downloaded phf_generator v0.8.0
  Downloaded lazy_static v1.4.0
  Downloaded itoa v1.0.1
  Downloaded instant v0.1.12
  Downloaded http-body v0.4.4
  Downloaded http v0.2.6
  Downloaded html2md v0.2.13
  Downloaded string_cache v0.8.3
  Downloaded percent-encoding v2.1.0
  Downloaded getrandom v0.1.16
  Downloaded futf v0.1.5
  Downloaded futures-core v0.3.21
  Downloaded futures-channel v0.3.21
  Downloaded fnv v1.0.7
  Downloaded cc v1.0.73
  Downloaded tracing-core v0.1.22
  Downloaded tokio-native-tls v0.3.0
  Downloaded tendril v0.4.2
  Downloaded string_cache_codegen v0.5.1
  Downloaded siphasher v0.3.9
  Downloaded serde_urlencoded v0.7.1
  Downloaded scopeguard v1.1.0
  Downloaded rand_core v0.5.1
  Downloaded phf_shared v0.10.0
  Downloaded openssl-probe v0.1.5
  Downloaded openssl-sys v0.9.72
  Downloaded mio v0.8.0
  Downloaded base64 v0.13.0
  Downloaded aho-corasick v0.7.18
  Downloaded syn v1.0.86
  Downloaded time v0.1.44
  Downloaded socket2 v0.4.4
  Downloaded rand v0.7.3
  Downloaded 104 crates (7.1 MB) in 4.93s (largest was `encoding_rs` at 1.4 MB)
   Compiling cfg-if v1.0.0
   Compiling libc v0.2.118
   Compiling log v0.4.14
   Compiling getrandom v0.1.16
   Compiling autocfg v1.1.0
   Compiling proc-macro2 v1.0.36
   Compiling siphasher v0.3.9
   Compiling unicode-xid v0.2.2
   Compiling memchr v2.4.1
   Compiling ppv-lite86 v0.2.16
   Compiling lazy_static v1.4.0
   Compiling pin-project-lite v0.2.8
   Compiling serde v1.0.136
   Compiling cc v1.0.73
   Compiling pkg-config v0.3.24
   Compiling futures-core v0.3.21
   Compiling parking_lot_core v0.8.5
   Compiling bytes v1.1.0
   Compiling smallvec v1.8.0
   Compiling new_debug_unreachable v1.0.4
   Compiling scopeguard v1.1.0
   Compiling futures-task v0.3.21
   Compiling mac v0.1.1
   Compiling itoa v1.0.1
   Compiling syn v1.0.86
   Compiling futures-util v0.3.21
   Compiling fnv v1.0.7
   Compiling openssl v0.10.38
   Compiling foreign-types-shared v0.1.1
   Compiling utf-8 v0.7.6
   Compiling precomputed-hash v0.1.1
   Compiling httparse v1.6.0
   Compiling futures-sink v0.3.21
   Compiling futures-io v0.3.21
   Compiling slab v0.4.5
   Compiling once_cell v1.9.0
   Compiling tinyvec_macros v0.1.0
   Compiling bitflags v1.3.2
   Compiling percent-encoding v2.1.0
   Compiling native-tls v0.2.8
   Compiling matches v0.1.9
   Compiling futures-channel v0.3.21
   Compiling pin-utils v0.1.0
   Compiling hashbrown v0.11.2
   Compiling try-lock v0.2.3
   Compiling openssl-probe v0.1.5
   Compiling encoding_rs v0.8.30
   Compiling unicode-bidi v0.3.7
   Compiling tower-service v0.3.1
   Compiling httpdate v1.0.2
   Compiling regex-syntax v0.6.25
   Compiling ryu v1.0.9
   Compiling base64 v0.13.0
   Compiling ipnet v2.3.1
   Compiling mime v0.3.16
   Compiling instant v0.1.12
   Compiling indexmap v1.8.0
   Compiling phf_shared v0.8.0
   Compiling phf_shared v0.10.0
   Compiling tracing-core v0.1.22
   Compiling openssl-sys v0.9.72
   Compiling lock_api v0.4.6
   Compiling futf v0.1.5
   Compiling http v0.2.6
   Compiling foreign-types v0.3.2
   Compiling tinyvec v1.5.1
   Compiling form_urlencoded v1.0.1
   Compiling phf v0.8.0
   Compiling tracing v0.1.31
   Compiling tendril v0.4.2
error: failed to run custom build command for `openssl-sys v0.9.72`

Caused by:
  process didn't exit successfully: `/root/scrape_url/target/debug/build/openssl-sys-a9db19d7be43a3ce/build-script-main` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR
  X86_64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_INCLUDE_DIR
  X86_64_UNKNOWN_LINUX_GNU_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR
  X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_DIR
  OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG
  cargo:rerun-if-env-changed=OPENSSL_STATIC
  cargo:rerun-if-env-changed=OPENSSL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
  cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_PATH
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
  cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
  run pkg_config fail: "Could not run `\"pkg-config\" \"--libs\" \"--cflags\" \"openssl\"`\nThe pkg-config command could not be found.\n\nMost likely, you need to install a pkg-config package for your OS.\nTry `apt install pkg-config`, or `yum install pkg-config`,\nor `pkg install pkg-config` depending on your distribution.\n\nIf you've already installed it, ensure the pkg-config command is one of the\ndirectories in the PATH environment variable.\n\nIf you did not expect this build to link to a pre-installed system library,\nthen check documentation of the openssl-sys crate for an option to\nbuild the library from source, or disable features or dependencies\nthat require pkg-config."

  --- stderr
  thread 'main' panicked at '

  Could not find directory of OpenSSL installation, and this `-sys` crate cannot proceed without this knowledge. If OpenSSL is installed and this crate had trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the compilation process.

  Make sure you also have the development packages of openssl installed.
  For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.

  If you're in a situation where you think the directory *should* be found automatically, please open a bug at https://github.com/sfackler/rust-openssl and include information about your system as well as this message.

  $HOST = x86_64-unknown-linux-gnu
  $TARGET = x86_64-unknown-linux-gnu
  openssl-sys = 0.9.72


  It looks like you're compiling on Linux and also targeting Linux. Currently this requires the `pkg-config` utility to find OpenSSL but unfortunately `pkg-config` could not be found. If you have OpenSSL installed you can likely fix this by installing `pkg-config`.

  ', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.72/build/find_normal.rs:180:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed

我觉得这是没有设置代理的错误,于是我使用export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static设置一下代理。再次执行cargo run,出现了下边的描述:

`  Compiling openssl-sys v0.9.72
   Compiling http-body v0.4.4
error: failed to run custom build command for `openssl-sys v0.9.72`

Caused by:
  process didn't exit successfully: `/root/scrape_url/target/debug/build/openssl                                                                                                             -sys-a9db19d7be43a3ce/build-script-main` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR
  X86_64_UNKNOWN_LINUX_GNU_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_INCLUDE_DIR
  X86_64_UNKNOWN_LINUX_GNU_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR
  X86_64_UNKNOWN_LINUX_GNU_OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_DIR
  OPENSSL_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=HOST_PKG_CONFI
<think>好的,我需要帮助用户在Ubuntu上运行非Python的内容。用户提到遇到了与Python无关的执行问题,比如之前提到的graphviz错误和TensorFlow的GPU错误,但这次明确说想运行非Python的东西,可能是C++、C或其他语言的项目。首先,我需要分析用户可能的需求,可能涉及编译、环境配置、依赖项安装等。 首先,用户可能在运行C/C++程序时遇到问题。比如编译错误,缺少库,或者执行权限问题。需要指导他们如何安装构建工具,比如gcc、make,以及如何管理依赖库。例如,使用apt安装必要的开发包。 其次,用户提到的graphviz错误可能涉及图形生成工具,可能需要安装正确的格式支持,比如PDF生成需要额外的软件包。虽然用户现在想运行非Python内容,但graphviz本身可能作为依赖被其他程序调用,需要确保系统安装了正确的依赖项。 另外,用户之前遇到的TensorFlow GPU错误可能涉及CUDA驱动和兼容性问题。如果用户现在运行的是其他需要GPU加速的非Python程序,可能需要检查NVIDIA驱动、CUDA工具包是否正确安装,以及程序是否针对当前GPU架构编译。 接下来,我应该分步骤说明如何在Ubuntu上运行非Python项目,比如C/C++程序的编译执行,处理依赖,解决常见错误。同时,需要提醒用户注意权限问题,使用chmod赋予可执行权限,或者使用sudo安装系统级依赖。 可能还需要提到包管理工具,比如apt、snap,或者从源码编译安装的步骤。此外,如果涉及容器化部署,可以简要提一下Docker的使用,但用户的问题可能更基础。 最后,生成相关问题,帮助用户扩展了解编译工具、依赖管理、调试方法等。</think>在Ubuntu上运行非Python程序时,常见场景可分为编译型语言(如C/C++/Rust)和脚本型语言(如Bash/Perl)。以下是分步解决方案: ### 一、编译型语言(以C++为例) 1. **安装构建工具链** ```bash sudo apt update && sudo apt install build-essential cmake ``` 这将安装$gcc$、$g++$、$make$等基础工具[^1] 2. **编译源代码** ```bash g++ -o myapp main.cpp # 单文件编译 mkdir build && cd build && cmake .. && make # CMake项目构建 ``` 3. **处理依赖缺失** 若报错`fatal error: xxx.h: No such file or directory`,需安装开发包: ```bash sudo apt install lib<name>-dev # 如libopencv-dev ``` ### 二、脚本型语言(以Bash为例) ```bash #!/bin/bash echo "Hello Ubuntu" ``` 1. **赋予执行权限** ```bash chmod +x script.sh ``` 2. **指定解释器** ```bash ./script.sh # 需要首行shebang声明 ``` ### 三、解决graphviz类问题 当遇到`Format "pdf" not recognized`错误时: ```bash sudo apt install graphviz graphviz-dev # 补充格式支持 export GRAPHVIZ_DOT=/usr/bin/dot # 确保路径正确[^2] ``` ### 四、GPU相关程序问题 针对`no kernel image available for device`类错误: 1. 验证驱动状态 ```bash nvidia-smi # 确认GPU识别正常 ``` 2. 检查CUDA兼容性 ```bash /usr/local/cuda/bin/nvcc --version # 查看编译器版本 ``` 3. 编译时指定计算能力 ```makefile GENCODE_FLAGS = -gencode arch=compute_75,code=sm_75 # 对应Turing架构 ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值