In alpine, rust can not link ssl library

My dockerfile is:

FROM rust:1.80.0-alpine3.20

LABEL com.github.actions.name="auto publish"
LABEL com.github.actions.icon="package"
LABEL com.github.actions.color="blue"

LABEL version="0.1.0"
LABEL repository="http://github.com/tu6ge/publish-action"
LABEL homepage="http://github.com/tu6ge/publish-action"
LABEL maintainer="tu6ge <772364230@qq.com>"

RUN apk update
RUN apk add libc-dev openssl-dev build-base musl-dev pkgconfig
#openssl-dev

WORKDIR /publish

COPY . /publish

#RUN export RUSTFLAGS='-C target-feature=-crt-static'
#RUN export RUSTFLAGS="-C link-arg=-static"
RUN cargo build

and my Cargo.toml

[package]
authors = ["tu6ge <772364230@qq.com>"]
description = "Auto Publish Cargo with Github Action"
edition = "2021"
keywords = ["github-action", "cargo", "CI-CD", "publish"]
license = "MIT"
name = "publish-action"
repository = "https://github.com/tu6ge/publish-action"
version = "0.4.0"

[[bin]]
name = "publish-action"

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

[dependencies]

[dev-dependencies]
dotenv = "0.15.0"
tempfile = "3.5.0"

[build-dependencies]
cc = "1.0"  

and my build.rs file is

fn main(){
    println!("cargo:rustc-link-search=native=/usr/lib");
    println!("cargo:rustc-link-lib=ssl");
    println!("cargo:rustc-link-lib=crypto");
}

and main.rs is

fn main()  {
    //list_top_dependencies();
    println!("hello world");
}

Result is link lssl error

/usr/lib/gcc/aarch64-alpine-linux-musl/13.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lssl: No such file or directory
/usr/lib/gcc/aarch64-alpine-linux-musl/13.2.1/../../../../aarch64-alpine-linux-musl/bin/ld: cannot find -lcrypto: No such file or directory

Don't print your own linking directives. Use the openssl-sys crate. It has ability to find the correct package on the system:

2 Likes

I tried use it in alpine,it have other error

What error?

1 Like

My project relies on the cargo library, and when cargo turns on the vendored-openssl feature, it indirectly relies on openssl-sys, which results in an error.

cargo = {version = "0.82.0", features = ["vendored-openssl"]}

error:

error: failed to run custom build command for `openssl-sys v0.9.103`
18.59 
18.59 Caused by:
18.59   process didn't exit successfully: `/publish/target/debug/build/openssl-sys-c4a4608f95339019/build-script-main` (exit status: 101)
18.59   --- stdout
18.59   cargo:rustc-check-cfg=cfg(osslconf, values("OPENSSL_NO_OCB", "OPENSSL_NO_SM4", "OPENSSL_NO_SEED", "OPENSSL_NO_CHACHA", "OPENSSL_NO_CAST", "OPENSSL_NO_IDEA", "OPENSSL_NO_CAMELLIA", "OPENSSL_NO_RC4", "OPENSSL_NO_BF", "OPENSSL_NO_PSK", "OPENSSL_NO_DEPRECATED_3_0", "OPENSSL_NO_SCRYPT", "OPENSSL_NO_SM3", "OPENSSL_NO_RMD160", "OPENSSL_NO_EC2M", "OPENSSL_NO_OCSP", "OPENSSL_NO_CMS", "OPENSSL_NO_COMP", "OPENSSL_NO_SOCK", "OPENSSL_NO_STDIO"))
18.59   cargo:rustc-check-cfg=cfg(openssl)
18.59   cargo:rustc-check-cfg=cfg(libressl)
18.59   cargo:rustc-check-cfg=cfg(boringssl)
18.59   cargo:rustc-check-cfg=cfg(libressl250)
18.59   cargo:rustc-check-cfg=cfg(libressl251)
18.59   cargo:rustc-check-cfg=cfg(libressl252)
18.59   cargo:rustc-check-cfg=cfg(libressl261)
18.59   cargo:rustc-check-cfg=cfg(libressl270)
18.59   cargo:rustc-check-cfg=cfg(libressl271)
18.59   cargo:rustc-check-cfg=cfg(libressl273)
18.59   cargo:rustc-check-cfg=cfg(libressl280)
18.59   cargo:rustc-check-cfg=cfg(libressl281)
18.59   cargo:rustc-check-cfg=cfg(libressl291)
18.59   cargo:rustc-check-cfg=cfg(libressl310)
18.59   cargo:rustc-check-cfg=cfg(libressl321)
18.59   cargo:rustc-check-cfg=cfg(libressl332)
18.59   cargo:rustc-check-cfg=cfg(libressl340)
18.59   cargo:rustc-check-cfg=cfg(libressl350)
18.59   cargo:rustc-check-cfg=cfg(libressl360)
18.59   cargo:rustc-check-cfg=cfg(libressl361)
18.59   cargo:rustc-check-cfg=cfg(libressl370)
18.59   cargo:rustc-check-cfg=cfg(libressl380)
18.59   cargo:rustc-check-cfg=cfg(libressl381)
18.59   cargo:rustc-check-cfg=cfg(libressl382)
18.59   cargo:rustc-check-cfg=cfg(libressl390)
18.59   cargo:rustc-check-cfg=cfg(libressl400)
18.59   cargo:rustc-check-cfg=cfg(ossl101)
18.59   cargo:rustc-check-cfg=cfg(ossl102)
18.59   cargo:rustc-check-cfg=cfg(ossl102f)
18.59   cargo:rustc-check-cfg=cfg(ossl102h)
18.59   cargo:rustc-check-cfg=cfg(ossl110)
18.59   cargo:rustc-check-cfg=cfg(ossl110f)
18.59   cargo:rustc-check-cfg=cfg(ossl110g)
18.59   cargo:rustc-check-cfg=cfg(ossl110h)
18.59   cargo:rustc-check-cfg=cfg(ossl111)
18.59   cargo:rustc-check-cfg=cfg(ossl111b)
18.59   cargo:rustc-check-cfg=cfg(ossl111c)
18.59   cargo:rustc-check-cfg=cfg(ossl111d)
18.59   cargo:rustc-check-cfg=cfg(ossl300)
18.59   cargo:rustc-check-cfg=cfg(ossl310)
18.59   cargo:rustc-check-cfg=cfg(ossl320)
18.59   cargo:rustc-check-cfg=cfg(ossl330)
18.59   cargo:rerun-if-env-changed=AARCH64_UNKNOWN_LINUX_MUSL_OPENSSL_NO_VENDOR
18.59   AARCH64_UNKNOWN_LINUX_MUSL_OPENSSL_NO_VENDOR unset
18.59   cargo:rerun-if-env-changed=OPENSSL_NO_VENDOR
18.59   OPENSSL_NO_VENDOR unset
18.59   cargo:rerun-if-env-changed=AARCH64_UNKNOWN_LINUX_MUSL_OPENSSL_CONFIG_DIR
18.59   AARCH64_UNKNOWN_LINUX_MUSL_OPENSSL_CONFIG_DIR unset
18.59   cargo:rerun-if-env-changed=OPENSSL_CONFIG_DIR
18.59   OPENSSL_CONFIG_DIR unset
18.59   OUT_DIR = Some(/publish/target/debug/build/openssl-sys-cd76d88bff820429/out)
18.59   cargo:rerun-if-env-changed=CC_aarch64-unknown-linux-musl
18.59   CC_aarch64-unknown-linux-musl = None
18.59   cargo:rerun-if-env-changed=CC_aarch64_unknown_linux_musl
18.59   CC_aarch64_unknown_linux_musl = None
18.59   cargo:rerun-if-env-changed=HOST_CC
18.59   HOST_CC = None
18.59   cargo:rerun-if-env-changed=CC
18.59   CC = None
18.59   cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
18.59   RUSTC_WRAPPER = None
18.59   cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
18.59   CRATE_CC_NO_DEFAULTS = None
18.59   DEBUG = Some(true)
18.59   CARGO_CFG_TARGET_FEATURE = Some(neon)
18.59   cargo:rerun-if-env-changed=CFLAGS_aarch64-unknown-linux-musl
18.59   CFLAGS_aarch64-unknown-linux-musl = None
18.59   cargo:rerun-if-env-changed=CFLAGS_aarch64_unknown_linux_musl
18.59   CFLAGS_aarch64_unknown_linux_musl = None
18.59   cargo:rerun-if-env-changed=HOST_CFLAGS
18.59   HOST_CFLAGS = None
18.59   cargo:rerun-if-env-changed=CFLAGS
18.59   CFLAGS = None
18.59   cargo:rerun-if-env-changed=AR_aarch64-unknown-linux-musl
18.59   AR_aarch64-unknown-linux-musl = None
18.59   cargo:rerun-if-env-changed=AR_aarch64_unknown_linux_musl
18.59   AR_aarch64_unknown_linux_musl = None
18.59   cargo:rerun-if-env-changed=HOST_AR
18.59   HOST_AR = None
18.59   cargo:rerun-if-env-changed=AR
18.59   AR = None
18.59   cargo:rerun-if-env-changed=ARFLAGS_aarch64-unknown-linux-musl
18.59   ARFLAGS_aarch64-unknown-linux-musl = None
18.59   cargo:rerun-if-env-changed=ARFLAGS_aarch64_unknown_linux_musl
18.59   ARFLAGS_aarch64_unknown_linux_musl = None
18.59   cargo:rerun-if-env-changed=HOST_ARFLAGS
18.59   HOST_ARFLAGS = None
18.59   cargo:rerun-if-env-changed=ARFLAGS
18.59   ARFLAGS = None
18.59   cargo:rerun-if-env-changed=RANLIB_aarch64-unknown-linux-musl
18.59   RANLIB_aarch64-unknown-linux-musl = None
18.59   cargo:rerun-if-env-changed=RANLIB_aarch64_unknown_linux_musl
18.59   RANLIB_aarch64_unknown_linux_musl = None
18.59   cargo:rerun-if-env-changed=HOST_RANLIB
18.59   HOST_RANLIB = None
18.59   cargo:rerun-if-env-changed=RANLIB
18.59   RANLIB = None
18.59   cargo:rerun-if-env-changed=RANLIBFLAGS_aarch64-unknown-linux-musl
18.59   RANLIBFLAGS_aarch64-unknown-linux-musl = None
18.59   cargo:rerun-if-env-changed=RANLIBFLAGS_aarch64_unknown_linux_musl
18.59   RANLIBFLAGS_aarch64_unknown_linux_musl = None
18.59   cargo:rerun-if-env-changed=HOST_RANLIBFLAGS
18.59   HOST_RANLIBFLAGS = None
18.59   cargo:rerun-if-env-changed=RANLIBFLAGS
18.59   RANLIBFLAGS = None
18.59   running cd "/publish/target/debug/build/openssl-sys-cd76d88bff820429/out/openssl-build/build/src" && env -u CROSS_COMPILE AR="ar" CC="cc" RANLIB="ranlib" "perl" "./Configure" "--prefix=/publish/target/debug/build/openssl-sys-cd76d88bff820429/out/openssl-build/install" "--openssldir=/usr/local/ssl" "no-dso" "no-shared" "no-ssl3" "no-tests" "no-comp" "no-zlib" "no-zlib-dynamic" "--libdir=lib" "no-md2" "no-rc5" "no-weak-ssl-ciphers" "no-camellia" "no-idea" "no-seed" "no-engine" "no-async" "linux-aarch64" "-O2" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "-DOPENSSL_NO_SECURE_MEMORY"
18.59 
18.59   --- stderr
18.59   thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/openssl-src-300.3.2+3.3.2/src/lib.rs:633:9:
18.59 
18.59 
18.59 
18.59   Error configuring OpenSSL build:
18.59       Command: cd "/publish/target/debug/build/openssl-sys-cd76d88bff820429/out/openssl-build/build/src" && env -u CROSS_COMPILE AR="ar" CC="cc" RANLIB="ranlib" "perl" "./Configure" "--prefix=/publish/target/debug/build/openssl-sys-cd76d88bff820429/out/openssl-build/install" "--openssldir=/usr/local/ssl" "no-dso" "no-shared" "no-ssl3" "no-tests" "no-comp" "no-zlib" "no-zlib-dynamic" "--libdir=lib" "no-md2" "no-rc5" "no-weak-ssl-ciphers" "no-camellia" "no-idea" "no-seed" "no-engine" "no-async" "linux-aarch64" "-O2" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "-DOPENSSL_NO_SECURE_MEMORY"
18.59       Failed to execute: No such file or directory (os error 2)

It looks like you haven't installed perl. Building OpenSSL requires perl.

Thank you very mush . Maybe my problem is solved

I hate the these low-quality build errors. Cargo should really try better here:

It's also unfortunate that Debug print of Command is so verbose it's hard to see the command name. I'm trying to work around that here:

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.