sfackler / rust-openssl

OpenSSL bindings for Rust
1.38k stars 740 forks source link

Failure to find appropriate headers on Ubuntu 24.04 despite having `libssl-dev` installed #2217

Open samuela opened 5 months ago

samuela commented 5 months ago

I'm currently seeing an error:

  cargo:warning=In file included from /usr/include/openssl/opensslv.h:109,
  cargo:warning=                 from build/expando.c:1:
  cargo:warning=/usr/include/openssl/macros.h:14:10: fatal error: openssl/opensslconf.h: No such file or directory
  cargo:warning=   14 | #include <openssl/opensslconf.h>
  cargo:warning=      |          ^~~~~~~~~~~~~~~~~~~~~~~
  cargo:warning=compilation terminated.

  --- stderr
  thread 'main' panicked at /root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/openssl-sys-0.9.101/build/main.rs:186:13:

  Header expansion error:
  Error { kind: ToolExecError, message: "Command \"cc\" \"-O0\" \"-ffunction-sections\" \"-fdata-sections\" \"-fPIC\" \"-gdwarf-4\" \"-fno-omit-frame-pointer\" \"-I\" \"/usr/include\" \"-Wall\" \"-Wextra\" \"-E\" \"build/expando.c\" with args \"cc\" did not execute successfully (status code exit status: 1)." }

  Failed to find OpenSSL development headers.

  You can try fixing this setting the `OPENSSL_DIR` environment variable
  pointing to your OpenSSL installation or installing OpenSSL headers package
  specific to your distribution:

      # On Ubuntu
      sudo apt-get install libssl-dev
...

Full log available in this gist.

However I have pkg-config and libssl-dev installed and up to date:

root@6b49c5e222e1:/workspaces/sshenanigans# apt-get install pkg-config libssl-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
pkg-config is already the newest version (1.8.1-2).
libssl-dev is already the newest version (3.0.13-0ubuntu3).
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
root@6b49c5e222e1:/workspaces/sshenanigans# 

Inspecting /usr/include, I can see that /usr/include/openssl/ has 133 header files, all the usual suspects, and /usr/include/aarch64-linux-gnu/openssl/ contains two header files: configuration.h and opensslconf.h.

For reproducibility, I'm attempting to cargo build a rust project that depends on openssl-sys from within this Docker container:

FROM ubuntu:24.04
RUN apt-get update && apt-get install -y curl libssl-dev pkg-config
# See https://github.com/DeterminateSystems/nix-installer?tab=readme-ov-file#in-a-container.
RUN curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux --init none --no-confirm
ENV PATH="${PATH}:/nix/var/nix/profiles/default/bin"
RUN nix profile install nixpkgs#cargo nixpkgs#gcc
samuela commented 5 months ago

I'm also able to reproduce the same behavior on Ubuntu 22.04

samuela commented 5 months ago

Using cargo from a rustup installation works however...

samuela commented 5 months ago

Ok, using cargo from rustup, but gcc from nixpkgs (nix profile install nixpkgs#gcc instead of apt-get install build-essential) produces the error. So I suspect something in thatcc` or its configuration is at fault...

dlime commented 2 months ago

With Ubuntu 24 and libssl-dev installed, I have solved this problem by symlinking the missing files from /usr/include/x86_64-linux-gnu/ to /usr/include/openssl/

sudo ln -s /usr/include/x86_64-linux-gnu/openssl/opensslconf.h /usr/include/openssl/opensslconf.h
sudo ln -s /usr/include/x86_64-linux-gnu/openssl/configuration.h /usr/include/openssl/configuration.h
lee-orr commented 2 months ago

This also happens when utilizing zig as a replacement for cc

secnigma commented 1 month ago

I've had the same issue while trying to build dog v0.1.0. The issue was solved by adding openssl as a dependency in the Cargo.toml file.

Line added under [dependencies]

[dependencies]
openssl = { version = "0.10.35", features = ["vendored"] }

After that try cargo build again.

Environment

OS: Debian GNU/Linux 12 (bookworm) Kernel: 6.9.7-1~bpo12+1 (2024-07-03) x86_64 GNU/Linux Cargo Version: 1.78.0 Rustc version: 1.78.0

Reference was taken from a Stackoverflow thread.