Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find the object file which contains the definition of printf function?

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.

like image 459
Ananda Avatar asked Jul 25 '12 16:07

Ananda


People also ask

Where is the definition of printf?

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)

Which header file contains definition of printf?

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.

Where is printf defined Linux?

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 .

Where does the function declaration and definition of printf () function reside?

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.


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.

like image 195
Kevin Vermeer Avatar answered Oct 12 '22 08:10

Kevin Vermeer


I found the exact answer to first two questions of mine -

  1. 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

  2. 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

  3. Still working on to find the correct answer.

like image 28
Ananda Avatar answered Oct 12 '22 08:10

Ananda