This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Linking with nonstandard malloc


On 4 January 2011 23:38, Amittai Aviram wrote:
> All of you are right on all counts. ?I was doing something too stupid to post here, fixed it, and now get the test message as desired. ?BTW, yes, I know that glibc's malloc is Doug Lea's malloc, but now I can further doctor my copy of Doug Lea's malloc to do special things.
>
> Point of information: ?malloc and the other routines (or rather dlmalloc, etc.) are thus defined both in my local source file (malloc.c) and in the glibc.a or glibc.so object code. ?How does the linker know to use my local malloc definitions instead of the ones in glibc--since I otherwise have to use glibc functions and am #including stdlib.h? ?Thanks!

If your program uses the functions malloc and free, but doesn't define
them, then it will have the symbols "malloc" and "free" in a list of
undefined symbols which the linker will attempt to satisfy, by finding
symbols with those names in the libraries specified on the linker
command line (which includes libc and any others you've specified with
-l options.)

The linker will look in each library in turn and use the first symbols
it finds with the right names.

If your program provides the definitions then the linker doesn't
bother looking for them in any libraries, and so doesn't find the ones
in libc.  If the linker finds symbols in some other library before it
looks in libc, it will stop looking and won't find the ones in libc.

It's a little more complicated than that, but that's the gist of it.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]