Reputation: 1971
I know that have been already asked a lot of time but I can't really solve it...
so I have a src
folder where my main.c
source is, an srclib
where my lib.c
file is stored and an include
directory where my lib.h
file is stored. now the makefile compiles the lib correctly and put the lib.a
file in lib
folder. the main.c
includes the lib like this:
#include "lib.h"
and it's compiled with -I ../include
option, but when i compile it I get the undefined reference to xxx
error for every function in the library
so what am I missing?
Upvotes: 0
Views: 2868
Reputation: 134276
Nope. -I
is for including the header files. You also need to link with the library using -l
option.
Note: You may need to provide the path-to-library using -L
option.
To quote the online gcc manual
-llibrary
Search the library named library when linking...... The linker searches a standard list of directories for the library, which is actually a file named
liblibrary.a
. The linker then uses this file as if it had been specified precisely by name.
EDIT:
To quote the remaining part of the same manual
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 libraryz
after filefoo.o
but beforebar.o
. Ifbar.o
refers to functions inz
, those functions may not be loaded.
So, you need to put the -l<libanme>
at the last of your compilation statement, as s_echo.c
uses functions defined in that particular library.
Upvotes: 5