0

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

Joster
  • 359
  • 1
  • 4
  • 19
  • What are you actually asking? The question contains details, but I can't find any description of what your actual problem is. – Cubic Sep 06 '16 at 13:16
  • Possible duplicate of [Building C-program "out of source tree" with GNU make](http://stackoverflow.com/questions/39015453/building-c-program-out-of-source-tree-with-gnu-make) – Tim Sep 06 '16 at 13:29
  • (You can find all what you need in my answer of the Q/A linked above. It's maybe a bit complicated but you should be able to adapt it to your structure) – Tim Sep 06 '16 at 13:30
  • Thank you, although your code pops up many questions to me, like i.e. what's the difference of doing SOURCEDIR := .. (Vs) SOURCEDIR=.. – Joster Sep 06 '16 at 13:45
  • 1
    You can find the explanation of "=" and ":=" [in the manual](http://www.gnu.org/software/make/manual/make.html#Flavors). – Beta Sep 07 '16 at 15:16

1 Answers1

1

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

Joster
  • 359
  • 1
  • 4
  • 19