You are not logged in.

#1 2012-06-11 20:31:18

ibrunton
Member
From: Canada
Registered: 2011-05-05
Posts: 270

[solved] gcc can't find my library

I'm trying to write a shared library (just for fun---it doesn't actually do anything especially useful at the moment).  I have the files libidb.h and libidb.c.  I build with the following:

gcc -c -Wall -Werror -fPIC libidb.c
gcc -shared libidb.o -o libidb.so

This works fine, no errors.

Then I have another program (in another directory) with this:

#include "idb.h"

When I try to run

gcc -L/absolute/path/to/library/folder -c -Wall -lidb mytest.c

I get the error

mytest.c:18:17: fatal error: idb.h: No such file or directory
compilation terminated
make: *** [mytest.o] Error 1

I'm following instructions from tutorials here and here, and I can't figure out what I'm missing.  I've tried running "ldconfig -n /path/to/library" and it makes no difference.  Any ideas?

Thanks.

Last edited by ibrunton (2012-06-11 21:21:40)

Offline

#2 2012-06-11 20:34:50

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [solved] gcc can't find my library

It doesn't give an error about not finding the library, it says it can't find the header file.  You either need to include the path in the include statement, or use the -I (capital i) flag to specify an include path.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2012-06-11 20:36:37

Internet Mouse
Member
Registered: 2012-06-11
Posts: 11

Re: [solved] gcc can't find my library

I am by no means an expert, but shouldn't libraries be included within <> tags ?

Last edited by Internet Mouse (2012-06-11 20:43:39)

Offline

#4 2012-06-11 20:39:00

ibrunton
Member
From: Canada
Registered: 2011-05-05
Posts: 270

Re: [solved] gcc can't find my library

Trilby wrote:

It doesn't give an error about not finding the library, it says it can't find the header file.  You either need to include the path in the include statement, or use the -I (capital i) flag to specify an include path.

I've tried the -I flag as well, and it doesn't change anything.

Offline

#5 2012-06-11 20:39:39

ibrunton
Member
From: Canada
Registered: 2011-05-05
Posts: 270

Re: [solved] gcc can't find my library

Internet Mouse wrote:

I am by no means an expert, but shouldn't libraries be included withing <> tags ?

I'm just going by what's in the tutorials, but I have tried it as <> instead of "", and I get the same error message.

Offline

#6 2012-06-11 20:45:02

Internet Mouse
Member
Registered: 2012-06-11
Posts: 11

Re: [solved] gcc can't find my library

ibrunton wrote:
Internet Mouse wrote:

I am by no means an expert, but shouldn't libraries be included withing <> tags ?

I'm just going by what's in the tutorials, but I have tried it as <> instead of "", and I get the same error message.

Have you tried to remove the extension ( .h ) ?

Offline

#7 2012-06-11 20:51:11

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [solved] gcc can't find my library

Oh dear, no it needs quotes, not brackets; it is a header file, not a library; and it most certainly does need the extension.

ibruton can you describe the directory structure where these files are stored.  You said that your app that is going to use the library is in a different directory, so I would not expect '#include "idb.h"' to work.  You may need '#include "../libSource/ibd.h"' or something of the sort to specify the actual location of the header file.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2012-06-11 20:58:10

debdj
Member
Registered: 2012-01-19
Posts: 163

Re: [solved] gcc can't find my library

you got to have/create the header file.

See this page: http://stackoverflow.com/questions/4966 … y-on-linux
may help.

Offline

#9 2012-06-11 21:21:21

ibrunton
Member
From: Canada
Registered: 2011-05-05
Posts: 270

Re: [solved] gcc can't find my library

Trilby wrote:

Oh dear, no it needs quotes, not brackets; it is a header file, not a library; and it most certainly does need the extension.

How does one distinguish a header file from a library?  I.e., how would one write/compile a library so that one would use <> in the #include statement?  Just curious.

Trilby wrote:

ibruton can you describe the directory structure where these files are stored.  You said that your app that is going to use the library is in a different directory, so I would not expect '#include "idb.h"' to work.  You may need '#include "../libSource/ibd.h"' or something of the sort to specify the actual location of the header file.

programming_directory
     |
    +- libidb/
     |      + idb.h
     |      + libidb.so
     |
    + mytest/
            + mytest.c

Once I double-checked my filenames (I originally had libidb.h, not idb.h), the -I (capital i) flag fixed the problem.  After some more error messages (linker could not find the library function called from mytest.c) and tinkering, I figured out what I need:

gcc -I/path/to/header/file -c -Wall -lidb mytest.c
gcc -L/path/to/library -lidb mytest.o

  i.e., header file for the compiler, library path for the linker.

Offline

#10 2012-06-11 21:30:01

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [solved] gcc can't find my library

Yup, looks like you've got it now.  It is possible to have one call to gcc compile and link - but it can be good practice to do them separately to understand the difference between the two processes.

The "<something.h>" bracket notation is really just a compiler shorthand.  The brackets tell the compiler to look in the system-wide include directories.  Using the -I flag to specify the path to your header may allow you to use the bracket notation (I think it should, I just haven't confirmed it).  You use brackets for stdlib.h and stdio.h and other common headers because the compiler knows where to look for them.  You could also use

// This is an example of a bad way of including std headers
#include "/path/to/std/libs/stdio.h"

but this would be very bad for portability: another system may have the headers in a different location.  The bracket notation tells the compiler to look for that file wherever the current systems headers are supposed to be.

Last edited by Trilby (2012-06-11 21:31:20)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB