GNU Make pattern para construir la salida en un directorio diferente al de src


Estoy intentando crear un Makefile que coloque mis archivos .o en un directorio diferente al de mis archivos fuente. Estoy tratando de usar una regla de patrón para no tener que crear reglas idénticas para cada archivo de origen y objeto.

La estructura de mi proyecto es algo así como:

project/
 + Makefile
 + src/
   + main.cpp
   + video.cpp
 + Debug/
   + src/       [contents built via Makefile:]
     + main.o
     + video.o

Mi Makefile se ve algo como:

OBJDIR_DEBUG = Debug
OBJ_DEBUG = $(OBJDIR_DEBUG)/src/main.o $(OBJDIR_DEBUG)/src/video.o

all: $(OBJ_DEBUG)

$(OBJ_DEBUG): %.o: %.cpp
    $(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c $< -o $@

Esto no funciona, porque busca mis archivos fuente en Debug/src/*.cpp.

He intentado lo siguiente:

# Broken: make: *** No rule to make target `Debug/src/main.cpp', needed by `Debug/src/main.o'.  Stop.
# As a test, works if I change "%.cpp" to "Debug/src/main.cpp", though it obv. builds the wrong thing

# Strip OBJDIR_DEBUG from the start of source files
$(OBJ_DEBUG): %.o: $(patsubst $(OBJDIR_DEBUG)/%,%,%.cpp)
    $(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c $< -o $@

# Broken:
#   Makefile:70: target `src/main.o' doesn't match the target pattern
#   Makefile:70: target `src/video.o' doesn't match the target pattern

# Add OBJDIR_DEBUG in target rule
OBJ = src/main.o src/video.o

$(OBJ): $(OBJDIR_DEBUG)/%.o: %.cpp
    $(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c $< -o $@
Author: mrb, 2012-11-25

3 answers

Después de releer la documentación de sobre reglas de patrón estáticas, derivé la siguiente regla de patrón que parece funcionar.

$(OBJ_DEBUG): $(OBJDIR_DEBUG)/%.o: %.cpp
    $(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c $< -o $@

No estoy seguro de que este sea el mejor enfoque, y estoy abierto a sugerencias.

 26
Author: mrb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-25 15:45:47

En lugar de construir objetos en otro directorio, podría intentar construir objetos desde fuentes en otro directorio: ponga su makefile en el directorio donde los objetos van a estar y dígale a make que busque fuentes en otro lugar usando VPATH. Esto funciona mejor si se supone que todos los archivos objeto terminan en el mismo directorio.

 3
Author: Bulletmagnet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-09 15:44:53

Makefile que "duplica" el árbol de fuentes en un directorio de compilación separado ejecutando GCC en cada fuente - https://stackoverflow.com/a/41924169/4224163

 0
Author: Alexey Semenyuk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:16:51