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

2

6

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.

Starinsky answered 25/7, 2012 at 16:22 Comment(2)
Its 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?Kean
I am looking for the object file under the libc which contains code definition of printf.Starinsky
B
4

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.

Bowra answered 25/7, 2012 at 16:35 Comment(2)
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.Starinsky
Sigh. Is this homework? (1) man ar, (2) objdump + some options | grep printf (3) See GCC documentation and try it.Bowra
S
1

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.

Starinsky answered 26/7, 2012 at 12:43 Comment(1)
Yeah, nm is a good alternative to objdump when you're dealing with libraries.Bowra

© 2022 - 2024 — McMap. All rights reserved.