I wrote a makefile (shown below) for a single-file C++ program in linux. I can successfully run the makefile if the source file and makefile are in the same directory. Trouble begins once my source file is moved to a different directory.
------------------------------------------
SRCS := bm.cpp
#VPATH = ./Top
INCLUDE_DIRECTORIES_MAIN = ./Top
CFLAGS =
GCC = g++
ALL_CFLAGS = $(addprefix -I,$(INCLUDE_DIRECTORIES_MAIN)) $(CFLAGS)
I tried using VPATH and also -Idir g++ option. I still get the following error:
-----------------------------
g++ -I./Top -c bm.cpp -o bm.o
g++: bm.cpp: No such file or directory
g++: no input files
make: *** [bm.o] Error 1
-----------------------------
Any suggestions are appreciated. I do not understand why the -I option is not working.
-I (dash-eye) is used only to specify the paths by which the compiler searches for header files. I think you are using it to try to tell the compiler what directories to search for the .cpp files you are trying to compile.
Thanks for pointing out jsmith. That was very helpful. I have another question.
I have many .cpp files in various folders. Is there a way to give a list of directories that make-rules can search from? Do you know whether VPATH can be used to do this?
Thanks for the help. I figured out that VPATH can be set to the list of directories containing source files. This would enable writing a cleaner makefile since you can list the sourcefile names without the complete path for each file, while indicating to the makefile the directories where it should look for source files.