G ++ compile source files existing in another directory

I am trying to customize the build process using make files for the C ++ plugin I'm working on. I am wondering if there is a way to make g ++ compile the source files found in another directory. My motivation for this is to not specify a relative path for each source file, as I explain below.

The structure of my project looks like this:

MyPlugin --> src --> Foo.cpp --> Foo.h --> Bar.cpp --> Bar.cpp --> build --> Makefile 

The 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 try to avoid defining the FILES variable, as shown below:

 FILES=../src/Foo.cpp ../src/Bar.cpp 

When I tried to run make all , g ++ gave me an error. It appears that the path specified with the -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 compiled. Another alternative is to cd in the src directory, as mentioned here , and run g ++ from there, but this only works for me if all the files are in the same one (I need the output file to be the only .so file). I also tried setting the PATH environment variable, but it showed no effect.

I went through the g ++ help system, make documentation, and looked at StackOverflow posts like https://stackoverflow.com/questions/10010741/g-compile-with-codes-of-base-class-in-a-separate-directory and gcc / g ++: "There is no such file or directory" , but could not find a solution that I could use. Could you suggest me a suitable approach to this problem?

Change Perhaps this example is 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 several files in each directory. Thus, although the cd in the src directory works in my example above, it will not work in my actual project (or at least I would like to know how it will work).

+4
source share
3 answers

You may have a second variable for the actual source files, for example:

 FILES = Foo.cpp Bar.cpp SOURCES = $(FILES:%.cpp=$(SRC_PATH)/%.cpp) 

Then, instead of using FILES when creating, use SOURCES .

+7
source

You can put all the source candidate directories in VPATH as follows:

 VPATH=${SRC_PATH}:other_source_dir1:other_source_dir2 

Then, when g ++ needs the source file, make will look for the source file by VPATH.

And try changing the list of .cc files to the .o list:

 FILES = file1.o file2.o 

And add the following two lines to your make file. Please note: the second line should begin with TAB.

 .cc.o: $(CC) $(CFLAGS) -c $< # NOTE This should begin with TAB 
+1
source

If you look at the output of Make on your console, you will see it as shown below:

 g++ -Wall -shared -I../src Foo.cpp Bar.cpp -o ../bin/MyPlugin.so 

As you can see, the path to the compiled files is incorrect. You already mentioned one way to achieve this by pre-specifying the original file names on .. / src (which is the problem with this approach). Or you can define the FILES as shown below:

 FILES=${SRC_PATH}/Foo.cpp \ ${SRC_PATH}/Bar.cpp 

Using the method described above, you do not need to manually edit the original path, which should change.

+1
source

All Articles