Joster
Joster

Reputation: 369

Makefile to compile shared library with folders

I need to write a Makefile to compile the next project:

project
   \_ src 
       \_ *.c
   \_ include 
       \_ *.h
   \_ build
       \_ Makefile
   \_ obj
       \_ output_library.so

I am new to Makefile's language and by now I have all the files in the same directory and compile with this Makefile:

SRC=../src                                                                      
OUTDIR=../obj     
CFLAGS=-ggdb -O1 -fPIC -Wall                                                                                                                
LDFLAGS=-shared -ggdb -fPIC -Wall                                                                                                           
all: libX.so                                                                                                                  

libX.so: X1.o X2.o X3.c                                                                
    $(CC) -o $@ $^ $(LDFLAGS)                                                   

clean:                                                                          
    rm *.so *.o || true

But i get prompted:

make: *** No rule to make target 'X1.o', needed by 'libX.so'.

What's obvious as i feel i'm not making use of $SRC and $OUTDIR

Upvotes: 0

Views: 908

Answers (1)

Joster
Joster

Reputation: 369

So I finally got it. Not the smartest way I'm afraid.

Makefile

SRC=../src
IDIR =../include
OUTDIR=../obj
CC=cc
CFLAGS=-I$(IDIR)
LDFLAGS=-shared -ggdb -fPIC -Wall -lnsl

ODIR=../obj
#LDIR =../lib

_DEPS = header.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = X1.o X2.o X3.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: $(SRC)/%.c $(DEPS)
        $(CC) -c -o $@ $< $(CFLAGS)

libX.so: $(OBJ)
        gcc -o $(ODIR)/$@ $^ $(CFLAGS) $(LDFLAGS)

.PHONY: clean

clean:
        rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~

I'm not sure about some of the statements anyway :/ but it works

Upvotes: 1

Related Questions