writing a FreeBSD C library
Harald Servat
redcrash at gmail.com
Wed Nov 4 09:27:32 UTC 2009
Hello Oliver,
2009/11/4 Oliver Mahmoudi <olivermahmoudi at gmail.com>
> 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.
>
>
I'm afraid that this is not the most common usage of libraries in the unix
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 at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"
>
--
_________________________________________________________________
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?!
More information about the freebsd-hackers
mailing list