Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/lacasseio/sample-as-needed-flag
- Owner: lacasseio
- Created: 2024-11-28T21:08:41.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2024-11-30T17:24:39.000Z (about 1 month ago)
- Last Synced: 2024-11-30T18:28:38.711Z (about 1 month ago)
- Language: Java
- Size: 49.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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:
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]
```