1
votes

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

2
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 the presence or absence of the files named in it. As this, make states that it cannot find the file include/functions.h. Double check it actually is here, with correct name and matching case.Didier Trosset
Thank you for the reply. But I am using the same directory structure as mentioned in the post and I had checked the spelling, matching case, but everything seems to be as shown in the post. I am still obtaining the same errorsunny
Just to check, is the + part of the name of the +include directory?Didier Trosset
For debugging purpose you could add the following lines to your Makefile: ./include/functions.h: and \ttouch $@. (Where \t is a tab character.) It will instruct make to create an empty include/functions.h file if it's missing.Didier Trosset
I just kept + to identify it as a directory in the post. Can you please give me the above command once.sunny

2 Answers

2
votes

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!

0
votes

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.