I\'m having a problem with my compiler telling me there is an \'undefined reference to\' a function I want to use in a library. Let me share some info on the problem:
I fear you mixed the library and header concepts.
Let's say you have a library libmylib.a
that contains the function myfunc()
and a corresponding header mylib.h
that defines its prototype. In your source file myapp.c
you include the header, either directly or including another header that includes it. For example:
/* myapp.h
** Here I will include and define my stuff
*/
...
#include "mylib.h"
...
your source file looks like:
/* myapp.c
** Here is my real code
*/
...
#include "myapp.h"
...
/* Here I can use the function */
myfunc(3,"XYZ");
Now you can compile it to obtain myapp.o
:
gcc -c -I../mylib/includes myapp.c
Note that the -I just tells gcc where the headers files are, they have nothing to do with the library itself!
Now you can link your application with the real library:
gcc -o myapp -L../mylib/libs myapp.o -lmylib
Note that the -L
switch tells gcc where the library is, and the -l
tells it to link your code to the library.
If you don't do this last step, you may encounter the problem you described.
There might be other more complex cases but from your question, I hope this would be enough to solve your problem.