Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lacasseio/sample-as-needed-flag

Demonstrate how to correctly add --as-needed flag to linked libraries
https://github.com/lacasseio/sample-as-needed-flag

Last synced: about 1 month ago
JSON representation

Demonstrate how to correctly add --as-needed flag to linked libraries

Awesome Lists containing this project

README

        

# Demonstrate how to add the --as-needed flag for all linked libraries

## Problem
The --as-needed flag is not used by default on GCC.
HOWEVER, some distribution provide GCC with a different [spec](https://gcc.gnu.org/onlinedocs/gcc/Spec-Files.html) (ex: ubuntu, alpine, debian).
To see the spec, use `g++ -dumpspecs` which will show under `link`.
You can also compile a simple application using `g++ --verbose` which will show the full linker commandl line.

You can see through Compiler Explorer the most raw specs:

Here

Aside from compiler explorer, we only found gcc:5.4 docker image to not provide the `--as-needed` flag.

```
$ docker run --rm -it -v $PWD:/workspace -w /workspace $(docker build -q .)
# ./gradlew assemble
# readelf -d app/build/exe/main/debug/app | grep NEEDED
0x0000000000000001 (NEEDED) Shared library: [liblib.so]
0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
# git apply use-plugin.patch
# ./gradlew assemble
# readelf -d app/build/exe/main/debug/app | grep NEEDED
0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
```

Running vanilla GCC using compiler explorer:
```
$ docker run --rm -it --platform linux/amd64 ubuntu:latest
# apt update && apt install -y make git curl python3 xz-utils
# apt install gcc-multilib # musl-dev on alpine
> 2 # America
> 102 # Montreal
# git clone https://github.com/compiler-explorer/infra.git && cd infra
# make ce
# ./bin/ce_install list # choose compiler
# ./bin/ce_install install compilers/c++/x86/gcc 14.2.0

# vim main.cpp
# /opt/compiler-explorer/gcc-14.2.0/bin/gcc main.cpp -lm -lcrypt
# readelf -d a.out | grep NEEDED
0x0000000000000001 (NEEDED) Shared library: [libm.so.6]
0x0000000000000001 (NEEDED) Shared library: [libcrypt.so.1]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
# /opt/compiler-explorer/gcc-14.2.0/bin/gcc main.cpp -Wl,--as-needed -lm -lcrypt
# readelf -d a.out | grep NEEDED
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
```