Makefile for all files

How can I write a makefile for my structure:

/ --
   /bin
   /out
   /src --
        /dir1
        /dir2
        ...
        /dirN
   main.cpp

I want:

  • Compile all files from the / src directory
  • All .out files in / out, without src directory structure
  • Compile main.cpp with all .out files

I have files .cand .cppto compile and link.

I tried:

${OBJ_DIR}/%.o: $(SRC_DIR)/%.cpp
    $(CXX) -c $< -o $@ ${CFLAGS}

But now I do not know how I can make a rule all...

+4
source share
1 answer

Here is the Makefile that I use in many projects:

# Compiler
CC   = g++
OPTS = -std=c++11

# Project name
PROJECT = foo_example_program

# Libraries
LIBS = -lpthread
INCS = -I./src/include

SRCS = $(shell find src -name '*.cpp')
DIRS = $(shell find src -type d | sed 's/src/./g' ) 
OBJS = $(patsubst src/%.cpp,out/%.o,$(SRCS))

# Targets
$(PROJECT): buildrepo $(OBJS)
    $(CC) $(OPTS) $(OBJS) $(LIBS) $(INCS) -o $@

out/%.o: src/%.cpp
    $(CC) $(OPTS) -c $< $(INCS) -o $@

clean:
    rm $(PROJECT) out -Rf

buildrepo:
    mkdir -p out
    for dir in $(DIRS); do mkdir -p out/$$dir; done

. , , src - , , . . , . , "object.1.o" "object.2.o" - .

+2

All Articles