asendjasni
asendjasni

Reputation: 1084

Using a static library in C

I found a useful library on github for my project, after building this later I tried to use some predefined function on it. I couldn't compile my project because there is some header file missing like this one :

In file included from main.c:2:0:
ptask.h:11:19: fatal error: ptime.h: No such file or directory

I compiled my project using this command :

gcc main.c -L. -lptask

This is all the files in project folder :

libptask.a  main.c  ptask.h

This is the library content:

$ ar -t libptask.a 
pbarrier.c.o
pmutex.c.o
ptask.c.o
ptime.c.o
rtmode.c.o
tstat.c.o
libdl.c.o
dle_timer.c.o
calibrate.c.o

Do I need to add all the headers of this files or just link the lib when compiling ?

Upvotes: 0

Views: 1256

Answers (3)

user3629249
user3629249

Reputation: 16550

regarding:

gcc main.c -L. -lptask

this is performing the compile step and the link step in one command.

It is also not enabling the warnings, which should always be enabled during the compile step.

Suggest something similar to the following to compile

gcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 -g -c main.c -o main.o -I.

and when you have fixed all the warnings, then use something similar to the following to link

gcc main.o -o main -L. -lptask

Upvotes: 1

LeleDumbo
LeleDumbo

Reputation: 9340

Your main.c #include-s ptask.h which in turn #include-s ptime.h. Having compiled static libs alone is not enough (that's the linker's job), you still need to have all used header files (which is the compiler's job), both the ones you use and their dependencies, recursively applicable.

Upvotes: 1

tadman
tadman

Reputation: 211740

Normally you need to be sure that the header files are in your "include path", something that a lot of compilers define with -I as a command-line option. You'll need to include the source directory of that library, or if it has a make install option, then the place where they got installed.

Upvotes: 1

Related Questions