I am a beginner in C, and i would like to read how various libraries are implemented. I looked under /usr/src/include and all i found was .h files. For example , i was looking at malloc.h and all that it does is , declare extern functions.
So i am trying to find the source for those functions. I downloaded gcc source rpm, but looking at the gcc source, it looks more like compiler code,rather than code for libraries such as stdlib.
Can you please help me by pointing me to the right direction.
Thank you.
You need to get the source code of the associated C library, probably glibc or eglibc in your case.
In the /usr/include/ folder, only the headers of the libc are present, along with some linux kernel headers in the linux/ subfolder.
You're looking for glibc, rather than gcc.
.h files never contains code (well, at least they should. Macros are exceptions)
the basic C functions are either in glibc or linux kernel
gcc, linux and glibc code are huge beasts, if you are a beginning beginner, you should go for simpler things
you probably should take a simple library, one that offer a few tools, on a narrow subject.
then go for linux and glibc (or uclibc)
I personnaly learnt many interesting stuff by reading microcontroller system libraries such as http://www.nongnu.org/avr-libc/, but this depends on what you need to do, and it requires a microcontroller to run it.
An idea: depending of what you're looking for, you could go for busybox: it is a reimplementation of many usefull system commands, you can learn a lot from it and running it do not require a dedicated computer.
Update: i asked a question related to glibc functions that got a few very interesting answers from my not-guru p.o.v:
where to find select() source code in glibc source?
Related
I have some code which relies on a library, namely the University of Tsukuba Elliptic Pairing Library. This library itself relies on GMP. I'd like to run this program baremetal on an ARM core, specifically on a beaglebone black.
GMP is a massive library, so I'd prefer to cross-compile if possible. I'd also prefer something other than the ``brute force" solution of simply dumping the entire GMP source code into the same file as my program and pushing it into arm-none-eabi-gcc.
What is the standard method for linking nontrivial libraries into baremetal code?
In this specific case, mini-gmp is sufficient, providing a bunch of functionality is removed from TEPLA. This is sufficient for my purposes, but hardly a satisfying answer -- my solution to linking a nontrivial library is to instead link a trivial library. Though my exact problem is solved, I'll be leaving this question open in case someone has any cleverer ideas.
Sometimes, I want to know the implementation of a c function. My editor is vim. I have try ctags and cscope, and man.
man 2|3 only tell me how to use a function.
Both ctags and cscope can just find some of the implementation of functions.
They all can't find some functions. especially some system function(calls).
If a function can be use by include some header file, is there any way easily find the implementation of a function,
select(2) is a system call (but I suggest using poll(2) instead - google for C10K problem to understand why I prefer poll over select). So it is really implemented inside the linux kernel. The libc contains a small stub function (translating the C argument convention to the syscall convention, then doing the real syscall with e.g. some SYSENTER machine instruction). You could look into the source code of MUSL Libc (I recommend MUSL libc because its source is much easier to read) or the real Gnu libc to see that wrapper function.
FD_SET is just a macro, defined in /usr/include/x86_64-linux-gnu/sys/select.h and really in /usr/include/bits/select.h
But you are very right to try to find out how software functions of Linux are implemented: take advantage that it is free software.
Actually, the syscall layer is well defined and quite stable (see the syscalls(2) man page, and read Advanced Linux Programming for more. Look also for the Posix standards). It is much more interesting to study the source code of higher-level libraries using them (e.g. Qt, Gtk, ...).
From an application's point of view, syscalls are elementary "atomic" operations. strace is a handy utility to find which syscalls are done by some process (or running program).
You won't get around pulling in the sources of the module providing the function's implementation.
For Linux most of the modules in use are open source, so access to the sources shall be possible.
Where to get the sources from depends on library and/or the distribution in use. This includes the kernel.
There are distributions which may include all sources. Gentoo is one of those.
For Debian based distros it is easy to pull a package's sources using the apt-get tool:
$ apt-get source <package-name>
Other distros may use other ways to provide sources. Perhaps fellow SO experts might like to comment/answer regarding those.
So I have a minimal OS that doesn't do much. There's a bootloader, that loads a basic C kernel in 32-bit protected mode. How do I port in a C library so I can use things like printf? I'm looking to use the GNU C Library. Are there any tutorials anywhere?
Ok, porting in a C library isn't that hard, i'm using Newlib in my kernel. Here is a tutorial to start: http://wiki.osdev.org/Porting_Newlib.
You basically need to:
Compile the library (for example Newlib) using your cross compiler
Provide stub-implementations for a list of system functions (like fork, fstat, etc.) in your kernel
Link the library and your kernel together
If you want to use functions like malloc or printf (which uses malloc internally), you need some kind of memory management and simplest working implementation of sbrk.
I strongly recommend against glibc. It is a beast.
Try newlib instead. Porting it to a new kernel is easy. You just need to write a few support functions, as explained here.
Another new kid on the block is musl which specifically aims to improve the situation in embedded space.
It's probably not the best choice for a beginner, though, since it's still pretty much work in progress.
Better look for a small libc, like uClibc. The GNU C library is huge. And as the comments tell, the first step is to get a C compiler going.
What are you trying to do? Building a full operating system is a job for a group of people lasting a few years... better start with something that already works, and hack on the parts that most interest you.
I study C and I noticed that I can't find the implementation file for some header files like, for example, stdio.h which is a library which contains a lot of input/output functions, like printf. Where can I find its implementation?
Download one of these:
glibc
uclibc
dietlibc
BSD libc
Or, even better, download several of these and compare their implementations. Of course, these are likely doing a lot of things different compared to your particular standard library implementation, but would still be quite interesting for e.g. non-platform-specific functionality such as sprintf.
You need to find the source code for a C standard library like glibc: http://www.gnu.org/s/libc/
You can download the source here: http://ftp.gnu.org/gnu/glibc/
It contains source for all the library functions.
For example here. Google is your friend - just search for stdio.c. But note that you should handle these as "one implementation of many possible" - you don't know how your compiler is doing it by reading those, you just get an idea of how it can be done.
On Ubuntu or other OS that uses aptitude for package management, you can use:
apt-get source libc6
for example.
Also, running gcc in verbose mode will tell you the details of the paths it is using. This should help you find which include and library paths it's using.
gcc -v
or
gcc -Wl,--verbose
If you install the Windows SDK, there is an option to include the standard library source code, so you can also see how it is implemented on Windows.
I'm interested in reviewing some of the functions included in the string library for c, but I can't find the source code (literally, i.e. functions and all) online and I have no idea where to find it.
Thanks.
EDIT:
the link provided by pmg shows those functions divided into c files, so it's about what I needed. Thanks.
Take a look at redhat glibc. It appears to be somewhat current.
You'll find it in the source code of the gcc compiler.
http://www.gnu.org/software/gcc/
Usually included with the compiler that you install so this may vary. Also depends on the operating system your running. If you're using windows, I recommend you run a Windows search for strings.c and if you're running linux then you can use the find command.
Disregard the file I linked to prior to this edit. I should have verified the code before sending it. It didn't apply to your question. Sorry
Maybe you're looking for GNU C string.h?
You can check the source in any standard libc implementation http://www.gnu.org/software/libc/