【问题标题】:Makefile fails with error no rule to make targetMakefile 失败并出现错误 no rule to make target
【发布时间】:2015-06-23 09:02:00
【问题描述】:

我正在努力创建一个有效的 make 文件。

我的结构

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

我的代码

# Define compiler
CC = g++

# Compiler flags
CFLAGS  = -g -Wall

# Build target executable
DIR = /src
INCLUDES = ../include
TARGET = main

all: $(TARGET)

$(TARGET): $(TARGET).cpp
    cd $(DIR) &
    $(CC) $(CFLAGS) -I $(INCLUDES) -o $(TARGET) $(TARGET).cpp

clean:
    cd $(DIR) &
    $(RM) $(TARGET)

我的错误

make: *** 没有规则来制作目标 main.cpp', needed bymain'。停下来。

编辑 在我的 main.cpp 中,我在顶部有这一行,这意味着我的 Makefile 可以找到:#include "pugixml.hpp"

【问题讨论】:

  • 考虑到你的文件,目标应该是$(TARGET): src/$(TARGET).cpp
  • 嗯。好的。让我试试
  • @mpromonet 这似乎更接近,但我明白了:cd /src & g++ -g -Wall -I ../include -o main main.cpp /bin/sh: line 0: cd: /src: No such file or directory clang: error: no such file or directory: 'main.cpp' clang: error: no input files make: *** [main] Error 1
  • DIR = /src => 这是您尝试cd的绝对路径
  • @peter 我得到同样的错误,即使我这样做DIR = src

标签: c++ makefile


【解决方案1】:

我认为这应该适合你。

所有路径都是相对于 Makefile 所在的文件夹给出的。这意味着需要为源依赖项指定源文件夹。

# Define compiler
CC = g++

# Compiler flags
CFLAGS  = -g -Wall

# Build target executable
SRC = src
INCLUDES = include
TARGET = main

all: $(TARGET)

$(TARGET): $(SRC)/$(TARGET).cpp
    $(CC) $(CFLAGS) -I $(INCLUDES) -o $@ $^ 

clean:
    $(RM) $(TARGET)

对命令使用制表符(而不是空格)。

$@ 解析为目标(在本例中为main)。

$^ 解析为所有依赖项(在本例中为src/main.cpp)。

无需cd 进入源文件夹。

【讨论】:

    猜你喜欢
    • 2021-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    相关资源
    最近更新 更多