GCC 12.2.0 Issues with `-ffunction-sections` Leading to Unused Sections Not Being Removed
Hey everyone, I'm running into an issue that's driving me crazy. I'm sure I'm missing something obvious here, but I'm having trouble with GCC 12.2.0 not optimizing my code properly when I use the `-ffunction-sections` flag. I expected that this would allow the linker to eliminate unused functions, but it seems that some of my functions are still being included in the final binary. I have a simple setup where I have multiple source files, and I'm conditionally compiling sections of code based on certain preprocessor definitions. Hereโs an example of one of my source files: ```c // my_lib.c #include <stdio.h> #ifdef USE_FEATURE_A void feature_a() { printf("Feature A is enabled\n"); } #endif void common_function() { printf("Common function called\n"); } ``` And in my `Makefile`, I compile with: ```makefile CC=gcc CFLAGS=-Wall -Wextra -ffunction-sections -fdata-sections LDFLAGS=-Wl,--gc-sections all: my_program my_program: my_lib.o $(CC) -o my_program my_lib.o $(LDFLAGS) my_lib.o: my_lib.c $(CC) $(CFLAGS) -c my_lib.c ``` When I compile this with `USE_FEATURE_A` defined, everything works as expected. However, if I remove the definition, the `feature_a()` function should be eliminated, but itโs still present in the final binary. I verified this with `nm` and saw that `feature_a` still appears in the output. I tried adding `-fomit-frame-pointer` and `-Os` to my `CFLAGS`, but it didnโt help. I also ensured that there are no other object files or libraries being linked that might keep the reference to `feature_a()`. Is there something Iโm missing regarding the flags or the way I'm linking my objects? Any insights would be appreciated! My development environment is Windows. How would you solve this? For context: I'm using C on Windows. Any ideas what could be causing this?