BillyJean
BillyJean

Reputation: 1587

building program with make and automatic dependencies

I have written a simple C++ program, and for the first time I want to compile and link it using a makefile. As a challenge I want to make a makefile, which lists all dependencies by itself. I am following this tutorial. My program consist of main.cpp, ext1.cpp and ext1.h. Following the tutorial, I have the following makefile

VPATH    = src include

CPPFLAGS = -o include

CC = gcc

SOURCES  = main.cpp \
       ext1.cpp



-include $(subst .c,.d,$(SOURCES))



%.d: %.c
    $(CC) -M $(CPPFLAGS) $< > $@.$$$$;                      \
    sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;     \
    rm -f $@.$$$$

When I run this I get the message: make: *** No targets specified and no makefile found. Stop. It is not clear to me what I am missing in my case?

Upvotes: 0

Views: 93

Answers (2)

Sagar Sakre
Sagar Sakre

Reputation: 2426

no make file found ? what name you have given for makefile? make sure its makefile or Makefile if you are just executing command make else you can pass file name to make like this

make -f yourmakefile

and changes suggested by Petr Budnik must work

Upvotes: 1

Beta
Beta

Reputation: 99144

You are trying to do too much at once.

Step 1. Your first makefile should build the executable without attempting automatic dependency detection.

VPATH = include src
CPPFLAGS += -Iinclude
CC = gcc

exec: main.o ext1.o
    $(CC) $^ -o $@

%.o: %.cc
    $(CC) -c $(CPPFLAGS) $< -o $@

main.o ext1.o: ext1.h

Step 2. Once that works perfectly, you can put the header dependencies in separate files:

makefile:

VPATH = include src
CPPFLAGS += -Iinclude
CC = gcc

exec: main.o ext1.o
    $(CC) $^ -o $@

%.o: %.cc
    $(CC) -c $(CPPFLAGS) $< -o $@

-include *.d

main.d:

main.o : ext1.h

ext1.d:

ext1.o: ext1.h

Step 3. Once that works perfectly, you can generate the dependency files automatically:

VPATH = include src
CPPFLAGS += -Iinclude
CC = gcc

exec: main.o ext1.o
    $(CC) $^ -o $@

%.o: %.cc
    $(CC) -c -MMD $(CPPFLAGS) $< -o $@

-include *.d

Upvotes: 3

Related Questions