dchin2
dchin2

Reputation: 1

how to add libraries to a make file

I'm editing an example file already in a library. Inside the directory of the example, there is a Makefile that I have been using to compile and run the example. I now want to add an additional library to this make file, I have tried copy and pasting the header and .c file into the library folder specified in the Makefile, but it is not finding it. Here is the Makefile code:

CPPFLAGS = -I../../include
CFLAGS = 
LDFLAGS = -L../../lib -L../../lib64
LIBS = -lbsapi

Biometry: main.c
    $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) main.c -o Biometry $(LIBS)

The library that I wanted to add came with a .h file and a .c file. I have added those to the ../../include directory and made sure to add this to my code right below the previous #include

#include <tpl.h>

I am not sure what I am missing? The program ran properly before the addition of this library.

Upvotes: 0

Views: 16803

Answers (1)

Marcus M&#252;ller
Marcus M&#252;ller

Reputation: 36317

Libraries are usually first built and installed to your system, so you can later add them to what seems to be LIBS in your Makefile (let's say your library's called "foo", -lfoo).

So you've got two options:

  1. The "this actually deserves the name library" path: Find or write a Makefile to turn foo.c into a libfoo.so, install that, and then use LIBS= -lbsapi -lfoo, or
  2. The "I'm including the source code in my source code and hope that everything builds correctly" path: add foo.o to the things that Biometry needs, and add a recipe on how to build foo.o from foo.c, and add foo.c to your sources:
CPPFLAGS = -I../../include
CFLAGS = 
LDFLAGS = -L../../lib -L../../lib64
LIBS = -lbsapi

Biometry: main.c foo.o
    $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) main.c foo.o -o Biometry $(LIBS)

foo.o: foo.c
     $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) foo.c -o foo.o $(LIBS)

Upvotes: 2

Related Questions