Моя структура проекта выглядит следующим образом:Как закодировать makefile, когда исходные файлы находятся в разных каталогах?
.
├── build
│ ├── makefile
└── src
├── subsys1
│ ├── includes
│ └── src_1.cpp
└── subsys2
├── includes
└── src_2.cpp
Я написал Makefile построить исполняемый «MyApp» из приведенных выше исходных файлов:
print-% : ; @echo $* = $($*)
# Application name
APP := myapp
# Set the directory to store the object files in
OBJDIR_R = _gnuRelease
OBJDIR_D = _gnuDebug
# Names of compiler and linker, and their associated switches
CXX:=g++
LINKER:=g++
CXXFLAGS+=-Wall -std=c++11 -pedantic -lpthread -m64
ifeq ($(DEBUG),1)
OBJDIR := $(OBJDIR_D)
CXXFLAGS += -g
else
OBJDIR := $(OBJDIR_R)
CXXFLAGS += -O2
endif
LDFLAGS =
# Set the name of the output executable
EXEFILE := LedaAP
VPATH = ../src/subsys1 ../src/subsys2
CPP_FILES = src_1.cpp src_2.cpp
OBJ_FILES = $(patsubst %.cpp,%.o,$(CPP_FILES))
DEP_FILES = $(patsubst %.cpp,%.d,$(CPP_FILES))
INC_DIRS := ../src/subsys1/includes ../src/subsys2/includes
INC_SWITCHES=$(foreach d, $(INC_DIRS), -I$d)
# This default target builds the executable by linking the object files
$(APP) : $(OBJ_FILES)
$(LINKER) $(CXXFLAGS) $^ -o [email protected]
# A rule to build an .o file from a .cpp file
%.o: %.cpp
$(CXX) $(CXXFLAGS) $(INC_SWITCHES) -c $< -o [email protected]
# Running 'make clean' deletes the executables and all the libraries.
.PHONY: clean
clean:
@echo Deleting executables and libraries. Use make clean_exe to delete just the executables.
@rm -f $(OBJDIR_D)/$(EXEFILE)
@rm -f $(OBJDIR_R)/$(EXEFILE)
@rm -rf $(OBJDIR_D)
@rm -rf $(OBJDIR_R)
В результате объектные файлы и исполняемые в сборке каталог:
build
├── makefile
├── myapp
├── src_1.o
└── src_2.o
Я на самом деле хочу:
build
├── _gnuRelease
│ ├── myapp
│ └── objects
│ ├── src_1.o
│ └── src_2.o
└── makefile
Как я мог это достичь?
Привет, Дэвид, у меня был аналогичный вопрос некоторое время назад. Пожалуйста, проверьте это сообщение: http://stackoverflow.com/questions/39015453/building-c-program-out-of-source-tree-with-gnu-make –
Спасибо, это очень полезное решение. – DavidA