LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   why I couldn't find the source code of printf function in glibc source? (https://www.linuxquestions.org/questions/programming-9/why-i-couldnt-find-the-source-code-of-printf-function-in-glibc-source-756574/)

famsinyi 09-20-2009 10:58 AM

why I couldn't find the source code of printf function in glibc source?
 
I'm trying to study how function in c++ library calls the kernel system call. I performed a 'grep' command to search the glibc-2.5 source code, but what I can find is only the function prototype of printf function as follow:

./libio/stdio.h:extern int printf (__const char *__restrict __format, ...);

I couldn't locate the function definition (implementation) of 'printf'

can someone help?

carbonfiber 09-20-2009 11:27 AM

Hello.

glibc (GNU C Library) is, as the name suggests, GNU's version of the C standard library (rather than C++). However, what arguments did you pass to grep in order to search for printf? Take a look inside stdio-common/printf.c

paulsm4 09-20-2009 01:37 PM

Hi -

Actually, "printf" is often implemented along with the C compiler. You'll definitely find "write()" (which printf uses) in glibc, but not necessarily "printf()" itself.

Try this:
Google printf.c+source

'Hope that helps .. PSM

PS:
You brought up an interesting point when you said "function in c++ library calls the kernel". As you see in the glibc source, the standard library is written in *C* (not "C++").

"C" is to "C++" very much what "write" is to "printf".

Make sense?

carbonfiber 09-20-2009 02:04 PM

Quote:

Originally Posted by paulsm4 (Post 3690796)
"printf" is often implemented along with the C compiler

That would pretty odd as it is normally not the compiler's job to provide it. Could you give examples of compilers that do?

Quote:

Originally Posted by paulsm4 (Post 3690796)
You'll definitely find "write()" (which printf uses) in glibc, but not necessarily "printf()" itself.

write() is not part of the standard C library. printf() is. In other words, in the absence of printf() - glibc could be considered incomplete to say the least.

paulsm4 09-20-2009 02:50 PM

Look, carbonfiber - I want to help famsinyi, not defend myself against you.

What you told him is true - thank you. What I told him is also true. If you doubt it, I suggest you try looking harder.

As far as "glibc-2.5": I confess, I don't know. As far as "C runtime libraries" in general: there's considerable diversity, depending on a) platform, b) vendor, c) version, d) many, many other factors.

famsinyi - please let us know what you find, and please post back if you have any questions.

Thanx in advance .. PSM

famsinyi 09-21-2009 09:06 AM

thank you very much paulsm4, carbonfiber. I think the definition should be located in stdio-common/printf.c. The marcro
ldbl_strong_alias (__printf, printf);
should have made a link within __printf and printf. I found a document giving some explanation about strong and weak alias. It could be found on the web with this keyword "reading glibc source code". The issue should be related to how assembler determines what symbol to use for a function. I'm not very sure now and need more study. I'll get back here after I really get the understanding.


All times are GMT -5. The time now is 06:37 AM.