Question

I would like to know if I can tell gcc/ld to omit unused modules from being put into the output file.

Suppose I have modules a.c, b.c and c.c. a.c and b.c depend on each other, one of them contains a main(), and due to whatever reasons, no parts of c.c are referenced.

gcc -c a.c
gcc -c b.c
gcc -c c.c

If I bundle the stuff together into a library, no code from c.c won't be in the output:

ar rcs abc.a a.o b.o c.o
gcc abc.a

But if I give the .o files directly to gcc, the code from c.c resp. c.o is included.

gcc a.o b.o c.o

Can I, by any way, tell gcc to leave out unused modules without putting them into a library?

I am programming an AVR µC and use AVR Studio, which doesn't allow the creation of libraries, but would like to omit the source files which are not used, depending on the build configuration.

Was it helpful?

Solution

I don't know if it's possible on AVR, but you could ask GCC to put each symbols in its own section using -ffunction-sections -fdata-sections at compile time. Then at link step, you could use -Wl,--gc-sections to ask ld to remove unused sections.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top