Jay
Jay

Reputation: 1038

Which functions are included in executable file compiled by gcc with "-static" on? And which functions are not?

When a C program is compiled with GCC's -static option on, the final executable file would include tons of C's standard functions. For example, my whole program is like

int main(int argc, char *argv[]) {
    printf("Hello, world!\n");
    return 0;
}

I checked the compiled executable file, and functions like, strcmp(), mktime(), realloc(), etc. are included in it, even though my program never calls them. However, some functions in stdlib.h are missing, like, rand(), system(), etc. My experiment environments are: Ubuntu 14.04 (with Linux kernel 3.13.0); GCC 4.8.2. I would like to know which C's functions would be included in the executable file when -static is turned on.

Upvotes: 2

Views: 131

Answers (2)

Petr Skocik
Petr Skocik

Reputation: 60068

Static libs are archives of object files. Linking them, only brings in those archive members that resolve undefined symbol references, and this works recursively (e.g., you may call a(), which calls b(), which calls c()). If each archive member defined exactly one symbol (e.g., a.o only defines a(), etc.) , you'd get only those symbols that were needed (recursively). Practically, an archive member may also define other symbols (e.g., a.o may define a() and variable), so you'll get symbols that resolve undefined symbol references along with the symbols that shared the same object file with the needed symbol definition.

Upvotes: 1

Makogan
Makogan

Reputation: 9536

Static linking means that ALL libraries that your program needs are linked and included into our executable at compiling time. In other words, your program will be larger, but it will be very independent (portable) as the executable will contain all libraries that it needs to run.

This means that with -static you will have ALL functions defined in your included libraries. You didn't put the include declarations, but just printf() already uses a large amount of libraries.

In other words, we cannot tell you which libraries are included in your program when using static, because it will vary from program to program.

Upvotes: 1

Related Questions