sunny
sunny

Reputation: 11

MakeFile : Header in different Directory - Error

I am trying to avoid relative paths in header declaration of C++ files. So, I had used the makefile by following an online example. But I am getting error. Please check the code and help me to resolve this

COMPILER = g++
IDIR = ./include
CXXFLAGS += -I$(IDIR)
CFLAGS += -I$(IDIR)

EXEC = hello
OBJECTS = main.o factorial.o hello.o

all: $(EXEC)

(---- I had also used CFLAGS instead of CXXFLAGS below but the same result###)

$(EXEC): $(OBJECTS)
    $(COMPILER) $(CXXFLAGS) $(OBJECTS) -o $(EXEC)  

main.o: main.cpp ./include/functions.h 
    $(COMPILER) $(CXXFLAGS) -c main.cpp

factorial.o: main.cpp ./include/functions.h 
    $(COMPILER) $(CXXFLAGS) -c factorial.cpp

hello.o: main.cpp ./include/functions.h 
    $(COMPILER) $(CXXFLAGS) -c hello.cpp

Error:

make: * No rule to make target 'include / functions.h " required by "main.o" to create. Closing.

Directory Structure is

- main.cpp
- factorial.cpp
- hello.cpp
- MakeFile.mk
- +include (dir) 
      ----->functions.h

main.cpp contains ----include "functions.h"---- in the header declaration

Upvotes: 1

Views: 4586

Answers (2)

Prettygeek
Prettygeek

Reputation: 2531

You are not using h files as a source files. there should only be:

main.o: main.cpp
$(COMPILER) $(CXXFLAGS) -c main.cpp

edited:

I copy your folder content and write simple application where hello.cpp have a simple void function, factorial.cpp have simple int-returning function ane main.cpp have int main() and uses this functions, include.h hafe declarations of these two dummy functions. Now. My makefile looks:

COMPILER = g++
IDIR = ./include
CXXFLAGS += -I$(IDIR)
EXEC =  hello
OBJECTS =  main.o factorial.o hello.o

all: $(EXEC)

$(EXEC): $(OBJECTS)
        $(COMPILER) $(CXXFLAGS) $(OBJECTS) -o $(EXEC)

main.o: main.cpp factorial.o hello.o
    $(COMPILER) -c $(CXXFLAGS) $^ -o $@

factorial.o: factorial.cpp
    $(COMPILER) -c $(CXXFLAGS) $^ -o $@

hello.o: hello.cpp
    $(COMPILER) -c $(CXXFLAGS) $^ -o $@

download for my sample

That should help You!

Upvotes: 2

Didier Trosset
Didier Trosset

Reputation: 37427

This is a pure make error message. The error is not related to the content of any .cpp or .h file. The error actually can come only from the content of the Makefile, and of the presence or absence of the files named in it.

As per this error message, make states that it cannot find the file include/functions.h. Double check it actually is here, with correct name and matching case.

For debugging purpose you can add the following lines to your Makefile:

./include/functions.h:
    touch $@

It will instruct make to create an empty include/functions.h file if it's missing.

Upvotes: 0

Related Questions