0
votes

I'm struggling to create a working make file.

My structure

  • root/Makefile
  • root/src/main.cpp
  • root/include/

My code

# 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)

My Error

make: *** No rule to make target main.cpp', needed bymain'. Stop.

EDIT Inside my main.cpp I have this line at the top which is meant to be found by my Makefile: #include "pugixml.hpp"

1
Considering your files, the target should be $(TARGET): src/$(TARGET).cppmpromonet
Hmm. Ok. Let me try thatJimmy
@mpromonet That seems a lot closer but I get this: 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 1Jimmy
DIR = /src => this is an absolute path to which you are trying to cdPeter Uhnak
@peter I get the same error even when I do DIR = srcJimmy

1 Answers

2
votes

I think this should work for you.

All paths are given relative to the folder the Makefile is in. That means the source folder needs to be specified for the source dependencies.

# 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)

Use tabs (not spaces) for the commands.

$@ resolves to the target (main in this case).

$^ resolves to all of the dependencies (src/main.cpp in this case).

No need to cd into the source folder.