Makefile 定义文件夹结构以查找程序并包含
Makefile defining folder structure to find program and includes
这是我的文件夹结构:
- root/Makefile
- root/src/main.cpp
- root/include
这是我的 Makefile:
# Define compiler
CC = g++
# Compiler flags
CFLAGS = -g -Wall
# Build target executable
DIR = src
TARGET = main
INCLUDES = include
all: $(TARGET)
$(TARGET): $(TARGET).cpp
$(CC) $(CFLAGS) -I $(INCLUDES) -o $(TARGET) $(TARGET).cpp
clean:
$(RM) $(TARGET)
我对如何将 $DIR 变量添加到我的目标感到困惑,我是否需要在 $TARGET 的每个实例上都这样做,并且它的格式是否如下所示:
$(DIR)/$(TARGET)
以下更改make 帮助您编译成功。您需要先进入 DIR 文件夹。所以,使用 cd $(DIR).
# Define compiler
CC = g++
# Compiler flags
CFLAGS = -g -Wall
# Build target executable
DIR = src
TARGET = main
INCLUDES = ../include
all: $(TARGET)
$(TARGET): $(TARGET).cpp
cd $(DIR) &
$(CC) $(CFLAGS) -I $(INCLUDES) -o $(TARGET) $(TARGET).cpp
clean:
cd $(DIR) &
$(RM) $(TARGET)
这是我的文件夹结构:
- root/Makefile
- root/src/main.cpp
- root/include
这是我的 Makefile:
# Define compiler
CC = g++
# Compiler flags
CFLAGS = -g -Wall
# Build target executable
DIR = src
TARGET = main
INCLUDES = include
all: $(TARGET)
$(TARGET): $(TARGET).cpp
$(CC) $(CFLAGS) -I $(INCLUDES) -o $(TARGET) $(TARGET).cpp
clean:
$(RM) $(TARGET)
我对如何将 $DIR 变量添加到我的目标感到困惑,我是否需要在 $TARGET 的每个实例上都这样做,并且它的格式是否如下所示:
$(DIR)/$(TARGET)
以下更改make 帮助您编译成功。您需要先进入 DIR 文件夹。所以,使用 cd $(DIR).
# Define compiler
CC = g++
# Compiler flags
CFLAGS = -g -Wall
# Build target executable
DIR = src
TARGET = main
INCLUDES = ../include
all: $(TARGET)
$(TARGET): $(TARGET).cpp
cd $(DIR) &
$(CC) $(CFLAGS) -I $(INCLUDES) -o $(TARGET) $(TARGET).cpp
clean:
cd $(DIR) &
$(RM) $(TARGET)