a.c
:
#include <openssl/ssl.h>
int main(int argc, char **argv) {
SSL_CTX *ssl_server_ctx = SSL_CTX_new(TLS_server_method());
return ssl_server_ctx ? EXIT_SUCCESS : EXIT_FAILURE;
}
$ docker run --rm -v "$PWD:/app" -w /app alpine:3.19 sh -euxc '
apk add build-base openssl-dev
gcc -lssl a.c
'
...
+ gcc -lssl a.c
/usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /tmp/ccghjdCK.o: in function `main':
a.c:(.text+0x10): undefined reference to `TLS_server_method'
/usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: a.c:(.text+0x18): undefined reference to `SSL_CTX_new'
collect2: error: ld returned 1 exit status
$ docker run --rm -v "$PWD:/app" -w /app alpine:3.19 sh -euxc '
apk add build-base openssl-dev
gcc a.c -lssl
'
...
+ gcc a.c -lssl
$ gcc -lssl a.c
$ gcc a.c -lssl
$ gcc --version
gcc (GCC) 13.2.1 20230801
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ docker run --rm alpine:3.19 sh -euxc 'apk add gcc; gcc --version'
...
+ gcc --version
gcc (Alpine 13.2.1_git20231014) 13.2.1 20231014
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ cat /etc/issue
Arch Linux \r (\l)
It appears that that's because on the host gcc
doesn't pass --as-needed
to the linker, but in the container it does:
$ docker run --rm -v "$PWD:/app" -w /app alpine:3.19 sh -euxc '
apk add build-base openssl-dev
gcc -v -lssl a.c
'
...
+ gcc -v -lssl b.c
...
/usr/libexec/gcc/x86_64-alpine-linux-musl/13.2.1/collect2 ... --as-needed ... -lssl /tmp/ccHJFmJh.o ...
/usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /tmp/ccHJFmJh.o: in function `main':
a.c:(.text+0x10): undefined reference to `TLS_server_method'
/usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: a.c:(.text+0x18): undefined reference to `SSL_CTX_new'
collect2: error: ld returned 1 exit status
$ gcc -v -lssl a.c
...
/usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/collect2 ... -lssl /tmp/cc59267x.o ...
COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64' '-dumpdir' 'a.'
When or what makes gcc add dependencies?
Why does the order in which libraries are linked sometimes cause errors in GCC?