rustrust-cargoreqwest

cargo build causes linking with cc failed because (maybe) openssl and reqwest


new to rust, I am trying to build a dummy reverse proxy with tokio and axum, you can find it here. While putting things together, I stumble in this error:

error: linking with `cc` failed: exit status: 1
  |
  ...
  = note: /usr/bin/ld: /home/fpezzati/workspace/keymaster/target/debug/deps/libreqwest-ca3141de8b4ae515.rlib(reqwest-ca3141de8b4ae515.reqwest.5838258711064664-cgu.12.rcgu.o): in function `openssl::ssl::SslStream<S>::ssl_read_uninit':
          /home/fpezzati/.cargo/registry/src/index.crates.io-6f17d22bba15001f/openssl-0.10.64/src/ssl/mod.rs:3786: undefined reference to `SSL_read_ex'
          ...
          /usr/bin/ld: /home/fpezzati/workspace/keymaster/target/debug/deps/libopenssl_sys-661b9af21fb0f20e.rlib(openssl_sys-661b9af21fb0f20e.openssl_sys.47e9ab06f8f5ff97-cgu.0.rcgu.o): in function `openssl_sys::openssl::ssl::SSL_CTX_set_max_proto_version':
          /home/fpezzati/.cargo/registry/src/index.crates.io-6f17d22bba15001f/openssl-sys-0.9.102/src/./ssl.rs:468: undefined reference to `SSL_CTX_ctrl'
          collect2: error: ld returned 1 exit status
          
  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

warning: `keymaster` (bin "keymaster") generated 6 warnings
error: could not compile `keymaster` (bin "keymaster") due to 1 previous error; 6 warnings emitted

It looks like something's wrong with my openssl and reqwest crate (they both appear in output), but I don't get what and cannot find any solution to this issue. I am trying to build this using rust 1.76, openssl 3.0.2 on a mint distro. I also tried to build the same on the latest rust docker image but I found other issues (cmake missing). I'll also appreciate any hint about how to deal/debug this kind of errors, I am new to rust and to system programming in general. Thanks.

UPDATE I added a minimal (as possible) example to reproduce the error:

use axum::{
    http::StatusCode, response::IntoResponse, routing::get, Json, Router
};
use serde::{Deserialize, Serialize};
use tokio::net::TcpListener;
use jwt_simple::prelude::RS384KeyPair;
use jwt_simple::prelude::Duration;
use jwt_simple::algorithms::RSAKeyPairLike;
use jwt_simple::claims::Claims;

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", get(helloproxy));

    let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

async fn helloproxy() -> impl IntoResponse {
    let client = reqwest::Client::new();
    match client.get("https://www.rust-lang.org").send().await {
        Ok(resp) => {
            let status = resp.status();
            let pkey = RS384KeyPair::from_pem("/tmp/valid.private.key").unwrap();
            let unsigned_claims = Claims::create(Duration::from_hours(2));
            let signed_claims = pkey.sign(unsigned_claims).unwrap();
            (status, Json(String::from("{ msg: 'hello' }")))
        },
        Err(e) => {
            (StatusCode::INTERNAL_SERVER_ERROR, Json(String::from("{ msg: 'hello' }")))
        }
    }
}

with these dependencies:

[dependencies]
axum = "0.7.5"
jwt-simple = "0.12.9"
reqwest = "0.12.5"
serde = "1.0.204"
tokio = { version = "1.38.1", features = [ "full" ] }

I saw problem rose up after using jwt-simple, maybe it helps. Please let me know if infos aren't enough, I am new to rust and system programming in general.


Solution

  • I solved my issue substituting jwt-simple dependency this way:

    jwt-simple = { version = "0.12.9", default-features=false, features = [ "pure-rust" ] }
    

    in my Cargo.toml. Because I am new to system programming, linking error was smoke to my eyes. Once I build the minimal reproducible example, I figured out jwt-simple was causing the issue. A careful reading of his crate page gave me solution:

    As a temporary workaround for portability issues with one of the dependencies (the boring crate), this library can be compiled to use only Rust implementations.

    In order to do so, import the crate with default-features=false, features=["pure-rust"] in your Cargo configuration.