Searching for object files...

Programming, for all ages and all languages.
Post Reply
User avatar
Civillian
Member
Member
Posts: 32
Joined: Tue Feb 21, 2012 3:26 pm

Searching for object files...

Post by Civillian »

This is the summary of my problem:
  • /lib/string/strcat.c -> /lib/string/strcat.o (ALL OK)
  • /lib/stdio/vsprintf.c -> /lib/stdio/vsprintf_test (executable, uses strcat() function, needs lib/string/strcat.o file, linker error!)
My question: how to tell GCC to search for suitable object files in a directory?

I know I could simply write its path in the command line, but this needs to go in the makefile instead.
I am using Solar's base makefile, which has dependency generation, if that can help.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Searching for object files...

Post by Solar »

Every good solution is obvious once you've found it.
User avatar
Civillian
Member
Member
Posts: 32
Joined: Tue Feb 21, 2012 3:26 pm

Re: Searching for object files...

Post by Civillian »

I tried that before starting the thread and it didn't work...
Must the "library" be an .a file like in your original makefile?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Searching for object files...

Post by Solar »

Civillian wrote:I tried that before starting the thread and it didn't work...
#-o

I should know better than to post before breakfast...

-L adds to the path searched for libraries (i.e., things you link in via -l). It doesn't apply here at all, sorry.

A "raw" .o object file (or a .a linker archive) is a different thing. You have to state those directly on the command line, i.e. there is no "search directory" (just like there is no such thing for sources).
Every good solution is obvious once you've found it.
User avatar
Civillian
Member
Member
Posts: 32
Joined: Tue Feb 21, 2012 3:26 pm

Re: Searching for object files...

Post by Civillian »

It seems to work if you create an .a file out of all the .o files (just like in your makefile) and then simply feed it to GCC as an "infile" as they call them in the manpage... just to never explain what exactly an "infile" can be...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Searching for object files...

Post by Solar »

The linker can take *.o files as input. A *.a file is simply an archive of multiple *.o files, and can be used like any other "infile" to the linker.

The idea behind *.a files is so you don't have to list the individual (and potentially numerous) *.o files; the linker will automatically pick the necessary *.o files from the *.a archive to satisfy any undefined references, and ignore the remaining *.o's.

I.e., *.a is a static (link-time) library, whereas a *.so (or *.dll) is a dynamic (run-time) library. Unless you have a dynamic library loader already implemented, you're stuck with static libraries, which is why I defaulted to them in the make tutorial.

Generally speaking, "man" pages are just for quick reference. If you really want the whole story, you should check out the manual of the tool in question. (Compare the 236 lines of "man make" with the 12,000 lines of the make manual.)
Every good solution is obvious once you've found it.
Post Reply