writing a FreeBSD C library

Gabor Kovesdan gabor at FreeBSD.org
Mon Oct 26 21:32:07 UTC 2009

Oliver Mahmoudi escribió:
> I compiled the library file in the following way.
> % gcc -I../include -Wall -c lb.c
> % ar rsv mylib.a lb.o
You can study bsd.lib.mk and bsd.prog.mk in /usr/share/mk. With these 
two includes you can deal easily with your C programs/libraries. It will 
serve you very well later.
> The compilation finished with no warnings  so I assume that there are no
> errors in the library itself.
> Trying to compile my source file - let's call it source.c - I always get the
> following error message:
> % gcc -o testfile source.c
> /var/tmp//ccQoff1S.o(.text+0x19): In function `main':
> : undefined reference to `myprintf'
> %
That's easy, your library isn't linked to your program. First, compile 
the source file but _do not link_:
gcc -c source.c
This will result in a source.o file. Then link the object files 
(source.o and the static library) with ld:
ld -o testfile source.o mylib.a
> In other words, gcc doesn't seem to find my library. I tried to mv my
> library file in /lib, /libexec
> /usr/lib, /usr/libdata and /usr/libexec but none of the above directories
> would directory would
> let me compile my source file.
With static libraries you can just specify the full path when linking.
> Now I am wondering where do I need to put mylib.a file to succeed?
> Also, this is a static library. What do I need to do under FreeBSD to
> compile this library into a dynamic
> library and where would I put this file?
Just using the proper parameters when compiling the library. It is a 
Linux article but you'll find some explications here:

Again, the bsd.*.mk Makefile includes are pretty handful.
> Lastly, most if not all the system calls can be found under /sys/kern/*.c.
> When writing a FreeBSD C program
> gcc makes use of the corresponding headers, e.g. unistd.h. and the standard
> library libc. Is it possible
> to peep a look at the source file for libc, i.e. is it included in the
> source tree? Where?
In /usr/src/lib/libc provided you have the source code installed in 
> There are many many more kernel related questions on my mind but I think
> that this is enought for one
> email.
I don't want to desperate you but these are very basic things. You 
should get a deeper knowledge of the basics first, before you try to do 
some kernel-related things. Otherwise, you will find it way too 
difficult and you won't enjoy. Anyway, the book of Marshall K. McKusick 
and George V. Neville-Neil is a good source of learning kernel stuff:


Gabor Kovesdan
FreeBSD Volunteer

EMAIL: gabor at FreeBSD.org .:|:. gabor at kovesdan.org
WEB:   http://people.FreeBSD.org/~gabor .:|:. http://kovesdan.org

More information about the freebsd-hackers mailing list