2
\$\begingroup\$

I am trying to make an easy-to-use non-recursive make script that works with several directories.

Everything is packed here.:

Directory structure

/           - root
/bin        - where executable(s) will be made
/obj        - where .o and .d files will be made
/include    - any external includes
/lib1       - library 1 plus its include files
              suppose we have four files here:
              file11.cc, file11.h
              file12.cc, file12.h
/main       - the program
              suppose we have one file here:
              main.cc
              but it includes file11.h and file12.h

Here are the makefiles:

/Makefile

MYCC        = g++

# ======================================================

CF_INCL_EXTRA   :=

CF_DEPS     = -MMD -MP
CF_INCL     = -Iinclude $(CF_INCL_EXTRA)
CF_OPTIM    =
CF_WARN     = -Wall -Wconversion -Wpedantic

CF_MISC     = -D_FILE_OFFSET_BITS=64

CF_ALL      = -std=c++11    \
            $(CF_DEPS)  \
            $(CF_INCL)  \
            $(CF_OPTIM) \
            $(CF_WARN)  \
            $(CF_MISC)

CXX     = $(MYCC) $(CF_ALL)

# ======================================================

LD_ALL      =
LL_ALL      = -lstdc++

LINK        = $(MYCC) $(LD_ALL) -o $@ $^ $(LL_ALL)

SRC     = $(wildcard *.cc)

# ======================================================

A       = bin/
O       = obj/

# ======================================================
# ======================================================
# ======================================================

SUBDIRS = lib1 main

include $(addsuffix /Makefile, $(SUBDIRS))

clean:
    rm -f \
        $(O)*.o     \
        $(O)*.d

/Makefile.sub

$(O)%.o: $(SRC_DIR)%.cc
    $(CXX) \
            -c $<       \
            -o $@

-include $(SRC:%.cc=%.d)

In each of the $(SUBDIRS), there must be a Makefile.

/lib1/Makefile

SRC_DIR := $(dir $(lastword $(MAKEFILE_LIST)))

CF_INCL_EXTRA += -I$(SRC_DIR)

include Makefile.sub

all: $(O)file11.o $(O)file12.o

/main/Makefile

SRC_DIR := $(dir $(lastword $(MAKEFILE_LIST)))

include Makefile.sub

all: $(A)main

$(A)main: $(O)main.o $(O)file11.o $(O)file12.o
    $(LINK)
\$\endgroup\$
2
  • \$\begingroup\$ Nothing wrong with what you have. Don't particularly like short variable names A and O but that's totally a personal preference. The only thing I would point out is that objects built with different options are not necessarily compatible (eg debug/release objects). \$\endgroup\$ Commented Apr 22, 2016 at 19:08
  • \$\begingroup\$ Currently this is the master makefile I use. This is an example of how to use it in this Makefile Currently its a bit of a mess because I am experimenting with Vera++ so ignore the lines with VERA_* \$\endgroup\$ Commented Apr 22, 2016 at 19:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.