0
votes

I'm trying to build my project from the top directory which have the subdirectories

root
Makefile
|    common/
|    lib/
|    app1/
|    app2/
|    app3/

Assumption 1: I'm at the root directory

Assumption 2: Each subdirectory has its own Makefile

Assumption 3: root's Makefile doesn't actually give any output, it only wants to execute Makefiles in the subdirectories

Assumption 4: common/ and lib/ will contain library files lib1.a and lib2.a

Now, by running make at the root, I want to first build the two libraries lib1.a and lib2.a. And then I want to build the respective apps in app1/, app2/, and app3/. The 3 apps should be linked to lib1.a and lib2.a.

I'm at the stage where I want to first build the two libraries by calling their Makefiles respectively. Here's what I have so far.

CFLAGS = -Wall -pedantic -std=c11 -ggdb
CC = gcc
MAKE = make

LIBPATH = lib/
COMMONPATH = common/

$(LIBPATH)lib1.a : $(LIBPATH)lib1.a   # i know this isn't appropriate but it executes make
    $(MAKE) -C $(LIBPATH)

$(COMMONPATH)lib2.a : $(COMMONPATH)lib2.a
    $(MAKE) -C $(COMMONPATH)

I know there are probably better ways to write this, but the main problem for me is that this makefile only executes whatever is first. Now it will recursveily run the lib1.a's makefile but not for lib2.a. As I want to write similar lines for app1 app2 and app3 I want to resolve this.

Thanks in advance!

1

1 Answers

1
votes

You need to declare the dependencies of your applications in your targets.

If app1, app2 and app3 are depends on lib1.a and lib2.a you need to write these dependencies in your target as well . The same for your libraries ( just in case that there are any dependencies existing between both libs as well ).

For instance:

LIBS = lib1.a lib2.a
SRC  = app1.cc
app1: $(SRC) $(LIBS) # you will say app1 depends on its source and the libs
    # compile your app