I am trying to setup a build process using makefiles, for a C++ plugin that I am working on. I am curious to know if there is any way to make g++ compile source files found in another directory. My motivation for doing this is to avoid having to specify the relative path for each source file, as I explain below.
My project directory structure looks like:
MyPlugin --> src --> Foo.cpp
--> Foo.h
--> Bar.cpp
--> Bar.cpp
--> build --> Makefile
Following is a stripped down version of my current Makefile:
SRC_PATH=../src
OUT_PATH=../bin
VPATH=${SRC_PATH}
FILES=Foo.cpp Bar.cpp
CC=g++
CFLAGS=-Wall -shared
all:
mkdir -p ${OUT_PATH}
${CC} ${CFLAGS} -I${SRC_PATH} ${FILES} -o ${OUT_PATH}/MyPlugin.so
By doing this, I am trying to avoid defining the FILES
variable as below:
FILES=../src/Foo.cpp ../src/Bar.cpp
When I tried running make all
, g++ gave me an error. It looks like the path specified through -I
flag is only used to search for #include
d files.
g++: Foo.cpp: No such file or directory
g++: Bar.cpp: No such file or directory
I cannot use wildcards (*.cpp
) because I do not always want all the files to be picked up for compilation. Another alternative is to cd
to the src directory, as mentioned here, and run g++ from there, but that only works for me if all the files are in the same directory (I need the output to be a single .so
file). I also tried setting the environment variable PATH
, but that didn't seem to have any effect.
I have looked into the g++ help, the make documentation, and looked at StackOverflow posts such as https://stackoverflow.com/questions/10010741/g-compile-with-codes-of-base-class-in-a-separate-directory and gcc/g++: "No such file or directory" , but could not find any solution which I could use. Could you please suggest me an suitable approach to this problem?
Edit The example may have been misleading. In this stripped down example, I have all the source files in one directory, but in my actual project, I have several subdirectories, and multiple files in each directory. Thus, although cd
ing to the src
directory works in my above example, it won't work in my actual project (or at least, I would like to know how it would work).
You can have second variable for the actual source files, like this:
FILES = Foo.cpp Bar.cpp
SOURCES = $(FILES:%.cpp=$(SRC_PATH)/%.cpp)
Then instead of using FILES
when building you use SOURCES
.
If you look at the output of Make on your console you'll it looks as shown below:
g++ -Wall -shared -I../src Foo.cpp Bar.cpp -o ../bin/MyPlugin.so
As you can see the path to the files to be compiled are not correct. You already mentioned one method of achieving this, by prefixing the source filenames with ../src (what's the issue with this approach). Or you can define FILES as show below:
FILES=${SRC_PATH}/Foo.cpp \
${SRC_PATH}/Bar.cpp
Using the method above you won't need to manual edit the source path should that change.
You can put all candidate source directories to VPATH like this:
VPATH=${SRC_PATH}:other_source_dir1:other_source_dir2
Then when g++ need a source file, make would look for the source file along the VPATH.
And try to do change .cc file list to .o list:
FILES = file1.o file2.o
And add following two lines in your make file. Note, the second line must begin with TAB.
.cc.o:
$(CC) $(CFLAGS) -c $< # NOTE This should begin with TAB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With