|
View:
New views
8 Messages
—
Rating Filter:
Alert me
|
|
|
writing a FreeBSD C libraryHello all y'all Kernel Hackers,
Trying to get a deeper understanding of the FreeBSD kernel I am currently I am studying C library files. For this reason I wrote a very simple library, just to understand how it works. Below are my source codes for: 1. the header 2. the library file 3. a simple C program with which I intend to test my library function ############## start of simple library header ######################## #ifndef _s_lib_h_ #define _s_lib_h_ #define SOME_INT 0x0001 /* prototype */ void myprnf(char *); #endif /* _s_lib_h_ */ ##################### end of header ############################ ################# start of library file lb.c ########################## #include <stdio.h> #include "slib.h" static void _myprf(char *); void myprnf(char *farg) { char *narg = farg; _myprf(narg); } static void _myprf(char *sarg) { char *pstr = sarg; printf("%s\n", pstr); } ############## end of library file ############################### ################ start of source file ############################ #include "slib.h" int main() { char *somestr = "hello world"; myprnf(somestr); return(1-1); } ################# end of source file ########################### I compiled the library file in the following way. % gcc -I../include -Wall -c lb.c % ar rsv mylib.a lb.o 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' % 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. 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? 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? Thanks Oliver There are many many more kernel related questions on my mind but I think that this is enought for one email. _______________________________________________ freebsd-hackers@... mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..." |
|
|
Re: writing a FreeBSD C libraryHi,
you have to link your executable to your library. The command-line option is -l. % gcc -o testfile -lmylib source.c Without it, your program doesn't know that this library exists (somewhere, /usr/lib, ...) Regards, Florian _______________________________________________ freebsd-hackers@... mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..." |
|
|
Re: writing a FreeBSD C libraryOliver 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' > % > > 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: http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html 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 /usr/src. > 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: http://www.amazon.com/Design-Implementation-FreeBSD-Operating-System/dp/0201702452 Regards, -- Gabor Kovesdan FreeBSD Volunteer EMAIL: gabor@... .:|:. gabor@... WEB: http://people.FreeBSD.org/~gabor .:|:. http://kovesdan.org _______________________________________________ freebsd-hackers@... mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..." |
|
|
Re: writing a FreeBSD C libraryThank you for your emails.
Neither one of the methods that you two suggested brought about the desired solution, but I have solved it. using gcc for the plain source with the -I switch gives: % gcc -o aprog aprog.c -I ~/mylib/ /var/tmp//ccHrDiyd.o(.text+0x19): In function `main': : undefined reference to `myprnf' creating an object file first and then linking with ld gives me: % ld -o aprog aprog.o mylib.a ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080 mylib.a(lb.o)(.text+0x33): In function `_myprf': : undefined reference to `puts' whereas placing mylib.a before the -o switch and then linking with ld gives: % ld mylib.a -o aprog aprog.o ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080 aprog.o(.text+0x19): In function `main': : undefined reference to `myprnf' which is essentially the same message it gives when compiling and linking with gcc in one step. The fact that the order of the arguments matters is also mentioned somewhere in gcc(1) and ld(1). The solution was to simply compile and link it like so: % gcc -o testfile aprog.c mylib.a % ./testfile hello world % This is in essence a synthesis of what you two suggested. Anyways, thanks again. Oliver > _______________________________________________ freebsd-hackers@... mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..." |
|
|
Re: writing a FreeBSD C libraryHello Oliver,
2009/11/4 Oliver Mahmoudi <olivermahmoudi@...> > Thank you for your emails. > Neither one of the methods that you two suggested brought about the desired > solution, but I have solved it. > > using gcc for the plain source with the -I switch gives: > % gcc -o aprog aprog.c -I ~/mylib/ > /var/tmp//ccHrDiyd.o(.text+0x19): In function `main': > : undefined reference to `myprnf' > > creating an object file first and then linking with ld gives me: > % ld -o aprog aprog.o mylib.a > ld: warning: cannot find entry symbol _start; defaulting to > 0000000008048080 > mylib.a(lb.o)(.text+0x33): In function `_myprf': > : undefined reference to `puts' > > whereas placing mylib.a before the -o switch and then linking with ld > gives: > % ld mylib.a -o aprog aprog.o > ld: warning: cannot find entry symbol _start; defaulting to > 0000000008048080 > aprog.o(.text+0x19): In function `main': > : undefined reference to `myprnf' > > which is essentially the same message it gives when compiling and linking > with gcc in one step. The fact that the order of the arguments matters is > also mentioned somewhere in gcc(1) and ld(1). > > The solution was to simply compile and link it like so: > % gcc -o testfile aprog.c mylib.a > % ./testfile > hello world > % > > > This is in essence a synthesis of what you two suggested. > > world. Libraries, typically, are called lib[SOMETHING].a (if they are static libraries) or lib[SOMETHING].so* (if they are shared libraries). In addition, the -l X option in the gcc compiler looks for libX.[a|so] in the all specified paths defined by -L, so in your first command gcc -o aprog aprog.c -I ~/mylib/ you're making gcc to look for for something called lib~/mylib/.[a|so] which I doubt it can be found. So, I suggest you to: 1.- Name your "mylib.a" into "libmylib.a" (or other name that begins with lib), 2.- add -L. to your 1st gcc invocation (in order to instruct gcc to look at the current directory, i.e., "."), and 3.- add -lmylib (if you called your library libmylib.a) to the gcc Your compile instruction, then, should look like gcc -o aproc aprog.c -L. -lmylib Regards. > > Anyways, thanks again. > > > Oliver > > > > > > _______________________________________________ > freebsd-hackers@... mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..." > -- _________________________________________________________________ Fry: You can see how I lived before I met you. Bender: You lived before you met me?! Fry: Yeah, lots of people did. Bender: Really?! _______________________________________________ freebsd-hackers@... mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..." |
|
|
Re: writing a FreeBSD C libraryHarald Servat <redcrash@...> writes:
> In addition, the -l X option in the gcc compiler looks for libX.[a|so] in > the all specified paths defined by -L, so in your first command > gcc -o aprog aprog.c -I ~/mylib/ > you're making gcc to look for for something called lib~/mylib/.[a|so] > which I doubt it can be found. You're confusing -l with -I... but the rest of your email is correct. DES -- Dag-Erling Smørgrav - des@... _______________________________________________ freebsd-hackers@... mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..." |
|
|
Re: writing a FreeBSD C libraryOh, yes! You're right DES. They look the same to me here in the web-browser
:) Oliver, regarding the Dag-Erling correction, the -I option in gcc refers to include header files (typically files ended with .h), not for naming libraries as I mentioned. Regards. 2009/11/4 Dag-Erling Smørgrav <des@...> > Harald Servat <redcrash@...> writes: > > In addition, the -l X option in the gcc compiler looks for libX.[a|so] > in > > the all specified paths defined by -L, so in your first command > > gcc -o aprog aprog.c -I ~/mylib/ > > you're making gcc to look for for something called lib~/mylib/.[a|so] > > which I doubt it can be found. > > You're confusing -l with -I... but the rest of your email is correct. > > DES > -- > Dag-Erling Smørgrav - des@... > -- _________________________________________________________________ Fry: You can see how I lived before I met you. Bender: You lived before you met me?! Fry: Yeah, lots of people did. Bender: Really?! _______________________________________________ freebsd-hackers@... mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..." |
|
|
Re: writing a FreeBSD C libraryOn Wed, 4 Nov 2009, Harald Servat wrote:
> Oh, yes! You're right DES. They look the same to me here in the web-browser > :) Oh, no. shoulda used a serif font! {:P > > Oliver, regarding the Dag-Erling correction, the -I option in gcc refers to > include header files (typically files ended with .h), not for naming > libraries as I mentioned. > > Regards. > > 2009/11/4 Dag-Erling Smørgrav <des@...> > >> Harald Servat <redcrash@...> writes: >>> In addition, the -l X option in the gcc compiler looks for libX.[a|so] >> in >>> the all specified paths defined by -L, so in your first command >>> gcc -o aprog aprog.c -I ~/mylib/ >>> you're making gcc to look for for something called lib~/mylib/.[a|so] >>> which I doubt it can be found. >> >> You're confusing -l with -I... but the rest of your email is correct. >> >> DES >> -- >> Dag-Erling Smørgrav - des@... >> > > > > -- > _________________________________________________________________ > Fry: You can see how I lived before I met you. > Bender: You lived before you met me?! > Fry: Yeah, lots of people did. > Bender: Really?! > _______________________________________________ > freebsd-hackers@... mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..." > Kayven Riese, BSCS, MS (Physiology and Biophysics) (415) 902 5513 cellular http://kayve.net Webmaster http://ChessYoga.org *----------------------------------------------------------* _______________________________________________ freebsd-hackers@... mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@..." |
| Free embeddable forum powered by Nabble | Forum Help |