I am using gcc compiler and ubuntu 12.04 OS. I want to know where can I find the object file and under which directory, which contains the definition of printf function. Again I am not looking for the header file which contains prototype but the object file which contains the actual definition.
-
1Its part of `libc`. What exactly are you looking for? As in are you looking for the definition in the source or the library which contains it on your machine? – another.anon.coward Jul 25 '12 at 16:26
-
I am looking for the object file under the libc which contains code definition of printf. – Ananda Jul 26 '12 at 04:27
2 Answers
Are you looking for the object file or the source file?
The .o object file is stored within a library, libc.so
. On most Linux distros, this file is located at /lib/libc.so
. On Ubuntu 11 and 12, as a part of multiarch support, the contents of /lib have been moved to /lib/i386-linux-gnu
and /lib/x86_64-linux-gnu
.
You can get the individual object file by using the ar
(archive) command that was used to create the library with the x
(extract) option:
ar x libc.a stdio.o
This doesn't seem very useful, though, so I'm guessing that you actually want the source file and not the object file. For that, you'd install the glibc package, which contains printf.c (which calls vprintf, which calls vfprintf, which contains the actual code for printf).
This source can be browsed on Launchpad. It's pretty complicated, and stretches well over 2000 lines of code.

- 2,736
- 2
- 27
- 38
-
I have 3 specific question in regard to this i.e (1) Using the ar command, list the all the object files that are present in libC 2. Among those object files, what is the file that contains the definition of the printf()? What is the command you used to find that out? 3. Is it possible to reduce the size of the final executable file by including only those definitions that are actually and needed / used in the program? If so, how. – Ananda Jul 26 '12 at 04:39
-
Sigh. Is this homework? (1) man ar, (2) `objdump + some options | grep printf` (3) See GCC documentation and try it. – Kevin Vermeer Jul 26 '12 at 10:23
I found the exact answer to first two questions of mine -
To list all object files present in libc we use following commands:
x86_64 system: $ ar -t /usr/lib/x86_64-linux-gnu/libc.a
i386 system: $ ar -t /usr/lib/i386-linux-gnu/libc.a
To find out which object file contain printf function run this command under /usr/lib/x86_64-linux-gnu /usr/lib/i386-linux-gnu or directory:
$ nm -o libc.a | grep -w printf
Still working on to find the correct answer.

- 1,572
- 7
- 27
- 54
-
1Yeah, `nm` is a good alternative to `objdump` when you're dealing with libraries. – Kevin Vermeer Jul 26 '12 at 13:42