Order matters in GCC sometimes

In my last post I mentioned having trouble with the order of command options in gcc. I finally found a mention of this in the documentation.

http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#index-l-1008

The relevant passage: It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, `foo.o -lz bar.o’ searches library `z’ after file foo.o but before bar.o. If bar.o refers to functions in `z’, those functions may not be loaded.

So, the libraries have to be stated after the source files that will need to refer to them, which is the opposite of what I thought it would be! Thankfully the error message was somewhat sensible, saying “undefined reference to…” which I know from past experience is a linker error as opposed to a header not being found (right?).

This entry was posted in Hobbies, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s