【问题标题】:Makefile defining folder structure to find program and includesMakefile 定义文件夹结构以查找程序并包含
【发布时间】:2015-04-16 08:51:07
【问题描述】:

这是我的文件夹结构:

  • 根/Makefile
  • root/src/main.cpp
  • 根目录/包含

这是我的 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)

【问题讨论】:

    标签: c++ makefile


    【解决方案1】:

    以下更改 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)
    

    【讨论】:

    • 谢谢。这是否意味着它现在正在寻找包含在 ~/src/include 而不是 ~/include 中的内容?
    • 是的,你需要设置 INCLUDES = ../include 这个。
    猜你喜欢
    • 2013-03-10
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    相关资源
    最近更新 更多