0

I have 2 directories in my project, one called Builds, who has the Makefile and a test program ( test-P0-consola.cpp ) and other one directory called P0 who constains the classes I use, cadena (string) and fecha (date).

test-P0-consola.cpp includes both of them, but Make doesn't find them.

CPP = g++
CPPFLAGS = -std=c++14 -g -Wall -pedantic

VPATH = ../P0:.:..

test-consola: test-P0-consola.o fecha.o cadena.o
    ${CPP} ${CPPFLAGS} -o $@.ex $^

test-P0-consola.o: test-P0-consola.cpp fecha.hpp cadena.hpp
    ${CPP} -c ${CPPFLAGS} $< -o $@

fecha.o: fecha.hpp
cadena.o: cadena.hpp

It throws the fatal error "cadena.hpp doesn't exist the file or directory" when it tries to compile test-P0-consola.o, but It find them out when I force it to compile cadena or fecha. I'm using GCC and Ubuntu.

..
├── Builds
│   ├── makefile.mak
│   └── test-P0-consola.cpp
├── P0
│   ├── cadena.cpp
│   ├── cadena.hpp
│   ├── fecha.cpp
│   └── fecha.hpp

EDIT

Error:

g++ -std=c++14 -g -Wall -pedantic -c test-P0-consola.cpp
test-P0-consola.cpp:7:21: fatal error: fecha.hpp: There is no file or directory

compilation terminated.

makefile.mak:9: Failure in the instructions for the objective 'test-P0-consola.o'

make: *** [test-P0-consola.o] Error 1
ildjarn
  • 62,044
  • 9
  • 127
  • 211
jramirez
  • 486
  • 9
  • 24
  • 1
    Can you describe the specific error and specific build you are doing, and print the full directory tree at the start of the build? It's unclear what the error is. – Barry Mar 17 '17 at 17:57
  • I added the full error as well as the directory tree. I'm just trying to execute a test that checks if the classes have some error. It works fine if you compile them in the same folder. – jramirez Mar 17 '17 at 18:17

2 Answers2

2

Look closely at your error:

test-P0-consola.cpp:7:21: fatal error: fecha.hpp: There is no file or directory

You probably have something like:

// test-P0-consola.cpp
#include "fetcha.hpp"

But fetcha.hpp is not in that directory, so it can't find it. You either need to change the way you include the file directly (via #include "../P0/fetcha.hpp") or to change the build rule to pass in an additional include path (via -I../P0).


Note: I'm not sure there's a reason to add . to VPATH. That's kind of implicit.

Note 2: this is a bad idea:

test-consola: test-P0-consola.o fecha.o cadena.o
    ${CPP} ${CPPFLAGS} -o $@.ex $^
                          ~~~~~

Don't lie to Make. The result of running a recipe should be the target file, except for PHONY targets. The recipe here should be -o $@. If you want the .ex suffix, you should change the target to be test-consola.ex. If you still want the rule to be named test-consola, you'll want:

test-consola : test-consola.ex
test-consola : .PHONY
Barry
  • 286,269
  • 29
  • 621
  • 977
1

You should put in the makefile the include path of the .hpp files you need the compiler to use. You should use the -Ipath compiler directive, where path is the path of your include files.

See `Makefile: How to correctly include header file and its directory?

and

How to define several include path in Makefile

Something like:

CPP = g++
CPPFLAGS = -std=c++14 -g -Wall -pedantic
INC = -Iyourincludebasepath/P0

VPATH = ../P0:.:..

test-consola: test-P0-consola.o fecha.o cadena.o
    ${CPP} ${CPPFLAGS} ${INC} -o $@.ex $^

test-P0-consola.o: test-P0-consola.cpp fecha.hpp cadena.hpp
    ${CPP} -c ${CPPFLAGS} ${INC} $< -o $@

fecha.o: fecha.hpp
cadena.o: cadena.hpp
Community
  • 1
  • 1
Mendes
  • 17,489
  • 35
  • 150
  • 263