I have 2 folders in my project, 1 called Builds that contains the Makefile and a test program (test-P0-console.cpp) and another called P0 with the date and string classes ( date.hpp / cpp and string.hpp / cpp ). The file test-P0-consola.cpp includes both classes, the problem is that since they are in different folders, the Makefile does not seem to find the date.hpp / chain.hpp, something that does not happen when they are both in the same folder.
CPP = g++
CPPFLAGS = -std=c++14 -g -Wall -pedantic
VPATH = ../P0:.:..
test-consola: test-P0-consola.o fecha.o cadena.o
${CPP} ${CPPFLAGS} -o [email protected] $^
test-P0-consola.o: test-P0-consola.cpp fecha.hpp cadena.hpp
${CPP} -c ${CPPFLAGS} $< -o $@
fecha.o: fecha.hpp
cadena.o: cadena.hpp
Both date.o and string.o compile by calling them explicitly. PS: Using Ubuntu and g++.
Before you fix that particular bug, you have other bugs in your makefile. First,
VPATH
you declare it but don't use it.Second,
CPP
it's not the C++ compiler, it's the preprocessor executable (cpp
, the program thatgcc
calls internally to deal with#include
,#define
, etc, before starting the compilation). Your C++ compiler should go inCXX
.It works for you because you have explicitly written
$(CPP)
in the body of the rule, and the value of the variable is being substituted, which effectively containsg++
. But it defeats the purpose of the variable (and besides, you can load the implicit rules ofGNU Make
).Same with
CPPFLAGS
, which are the preprocessor options. Yours should go inCXXFLAGS
:Now, the error for which you ask. In the makefile you have to put the complete relative path of the files that you put as a dependency:
And still this will not solve your problem. By putting the file
test-consola.cpp
outside of the folderP0
, and assuming that in the first few lines oftest-P0-consola.cpp
you'll have something like:It will turn out that the compiler will not be able to find where the files are
fecha.hpp
andcadena.hpp
fromtest-P0-consola.cpp
, and will give you a fatal no file error.Assuming the folder you're building from is
/home/tu_usario/POO/practicas/
, and from that folder, you build with:Any file that you include in
.cpp
this must include the relative path with respect to the folder where it is locatedtest-P0-consola.cpp
. If it is in the same folder, you can directly put#include "fecha.hpp"
. If it is in a different folder (P0
in your case), you have to modify ittest-P0-consola.cpp
with the correct relative path:But in case
test-P0-consola.cpp
you can't modify it —teachers' code, can't you? ;), the easiest thing to do is to tellg++
, which internally calls the preprocessor,PATH
alternative s where it can find more files in case it doesn't find them by relative paths. It is done with the option-I
(once for each folder):And
VPATH
I have told you about it because I suppose it must have been one of the attempts to add new folders to the path (what you have to do with-I
). Unfortunately,VPATH
it's not one of the implicit variables inGNU Make
, and even if it were, if you create your own rules and don't use the variable, the variable isn't used. Implicit variables only work for implicit rules, or explicit rules where you manually write them (such asCXX
, which defaults tog++
, andCXXFLAGS
, which defaults to empty).NOTE: By changing the variable
CPP
, you are not changing the call itgcc
makes internally to the preprocessor. It is simply for internal use by the makefile (in case you want to create rules to preprocess files manually instead of havinggcc
it preprocess for you).