I have two very simple C example files written which are called main.c
and simple_library.c
with simple_library.h
.
File main.c
:
#include "simple_library.h"
int main(int argc, char* argv[]) {
func_b(1, 2);
return 0;
}
File simple_library.h
:
#ifndef SIMPLE_LIBRARY_H
#define SIMPLE_LIBRARY_H
void func_a(int arg1, char arg2, void* arg3);
void func_b(char arg1, int arg2);
void func_c(char arg1);
#endif // SIMPLE_LIBRARY_H
File simple_library.c
:
#include "simple_library.h"
void func_a(int arg1, char arg2, void* arg3) {
(void) arg1;
(void) arg2;
(void) arg3;
return;
}
// in func_b the func_c is called
void func_b(char arg1, int arg2) {
(void) arg1;
(void) arg2;
func_c(0);
return;
}
void func_c(char arg1) {
(void) arg1;
return;
}
I am compiling it with the following commands:
gcc -Wall -c simple_library.c -o simple_library.o
gcc -Wall main.c simple_library.o -o main.o
After compilation, I just realized that by calling objdump -d main_simple_library.o > objdump_main_simple_library.txt
and seeing the output file that the function func_a
was still there in the static compiled binary.
Even if the function func_a
was never used in the code, I really wanted to get rid of it from the binary itself.
I have also tried to compile it with the -O2
flag and the function func_a
was still there in the binary.
My question would be as the following:
How can the compilation be optimized so that only the stuff (functions, variables etc.) are used in the finished static compiled binary which are actually used at the end? (starting from the main function)
I have also uploaded the files to my GitHub repo, which can be examen here (names could differ a bit).