开发者

gcc static link

开发者 https://www.devze.com 2023-03-08 19:48 出处:网络
I want to static link libray which I included (such as stdio) with gcc, so I use the -static options.

I want to static link libray which I included (such as stdio) with gcc, so I use the -static options.

My environment is ubuntu 10.10.

gcc version is 4.4.开发者_C百科5.

the compile command I used is : gcc -static -o output.out input.c

the following is my source code.

include

int main(){

printf("hello world");

return 0;

}

After I compile it and use the -static option, I objdump the executable file.

and I found out that the printf is actually called _IO_printf.

And I write another program, the following is the souce code.

include

int main(){

return 0;

}

I compile this source code with the same option and objdump the new executable file.

However, I can't find the _IO_printf.

My question is why I can't fine _IO_printf in the second case. I have static linked the libray which I included.

Can someone plz help me solve this problem, thx.


A linker doesn't just put object files and libraries together. It creates links between the different parts. So if there is an unresolved symbol (e.g. function or variable) in one unit, it looks for it in other units and makes the connection.

Since the second program doesn't call printf, the linker does not need to resolve that symbol. So there is no point adding that function to the executable (it'll just sit there and take space). The linker can see what's missing, and should (normally) add only what's missing down to some practical granularity.

0

精彩评论

暂无评论...
验证码 换一张
取 消