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.
printf is a C function belonging to the ANSI C standard library, and included in the file stdio. h. Its purpose is to print formatted text to the standard output stream. Hence the "f" in the name stands for "formatted". It takes the following unusual syntax: printf(string format, items-to-format)
printf() is formatted output function which is used to display some information on standard output unit. It is defined in standard header file stdio. h . So, to use printf() , we must include header file stdio.
printf is part of standard library, which is called "standard" because it is linked by default by C compiler. Standard library is typically provided by operating system or compiler. On most linux systems, it is located in libc.so , whereas on MS Windows C Library is provided by Visual C runtime file msvcrt. dll .
printf function is defined in stdio. h header file. So if we want to use the printf function in our c program, we should include stdio. h header file.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With