Friday, May 18, 2007

quirk of static linking

A question related to linking today.
Why is it advised to put the library options at the end of command line for compilation?

Hint: If we have a static library, say libfoo.a which we want to link to our program hello.c

cc hello.c -lfoo
rather than
cc -lfoo hello.c

-l option tells the compiler to link to library [lib]foo. Note that "lib" from libfoo is dropped and only "foo" part is given with -l.


toby said...

Yes, that's what the option does. But I thought you were going to tell us why cc -o foo foo.c -lthing and not cc -lthing -o foo foo.c (which work identically for me (gcc)).

osgeek said...

Did you try linking to a static library?
The explanation will be in the next post :-)

toby said...

No, I didn't. D'oh.