5

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 #included 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 cding 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).

Community
  • 1
  • 1
Masked Man
  • 1
  • 7
  • 40
  • 80
  • why does the output being a single .so file hinder you from cd 'ing into the `src` directory? – dinesh Nov 30 '12 at 05:36
  • Like I have mentioned, that only works if all the source files are in the same directory. I want all the source files to be compiled together into one .so file. – Masked Man Nov 30 '12 at 05:47

3 Answers3

7

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

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
TieDad
  • 9,143
  • 5
  • 32
  • 58
  • Thanks for your reply. I have already used the `VPATH` variable, but that doesn't seem to be working. The g++ command (echoed by make) looks like: `g++ -Wall -shared -I../src Foo.cpp Bar.cpp -o../bin/MyPlugin.so` It doesn't have `src/Foo.cpp src/Bar.cpp` – Masked Man Nov 30 '12 at 05:31
  • I have added two additional steps in answer, please try. – TieDad Nov 30 '12 at 07:09
1

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.

dinesh
  • 765
  • 2
  • 9
  • 21
  • Well, I want to know if the g++ command can automatically have `src/Foo.cpp src/Bar.cpp`. The g++ `-I` flag allows you to use `#include "Foo.h"` rather than `#include "../foo/Foo.h"`. I am trying to see if there's a similar way for the source files. As I mentioned, the approach works well if all the source files are in the same directory, but my project has several subdirectories and multiple files in each directory. It becomes cumbersome. If possible, I would prefer to just specify the file name, and configure `make` or `g++` to figure out where it is. – Masked Man Nov 30 '12 at 05:45