Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a makefile into an other makefile?

Tags:

c++

makefile

g++

I have some C++ classes, every one into his own folder with a makefile, test.cpp for testing purpose, etc.

Main folder
 |-> Class1 folder
 |-> Class2 folder
      |-> Class2.1 folder
 |-> Class2 folder

I have a main project, that must include these classes. I am trying to include all sub-makefiles into the main makefile.

I have tried with "INCLUDE POO/makefile", but this solution has 2 problems:

  • The path of the sub-makefiles is incorrect, so files are not found ("There is not rule to build the target 'Vector3D.cpp'").
  • The "test.cpp" file is overrided, probably because of the path problem.("warning: overriding recipe for target ...")

I would like all makefile independent, so I can copy/paste the class folder into a new project and it still working, or I may exec the makefile alone without changes on it.

So the question is: How to include (correctly) a makefile into another makefile?

Example, just for test purpose.

Main makefile (simplified)

include Vector3D/makefile
include Color/makefile

CPP      = g++
CXXFLAGS = $(CXXINCS) -Wall -O0

all: main

main: main.o Vector3D.o Color.o
    $(CPP) main.o Vector3D.o Color.o -o main

main.o: main.cpp
    $(CPP) -c main.cpp -o main.o $(CXXFLAGS)

Sub-makefile example (simplified)

#Vector3D
CPP      = g++
CXXFLAGS = $(CXXINCS) -Wall -O0


all: test

test: Vector3D.o test.o
    $(CPP) Vector3D.o test.o -o test

Vector3D/test.o: test.cpp
    $(CPP) -c test.cpp -o test.o $(CXXFLAGS)

Vector3D.o: Vector3D.cpp Vector3D.hpp
    $(CPP) -c Vector3D.cpp -o Vector3D.o $(CXXFLAGS)

Similar for Color/makefile than Vector3D.

like image 431
Adrian Maire Avatar asked Oct 10 '13 22:10

Adrian Maire


1 Answers

I would like all makefile independent, so I may copy/past the class folder into a new project and it still working, or I may exec the makefile alone without changes on it.

I'm pretty sure that's not possible. Your makefiles either need to be standalone, in which case you can make the top-level files invoke make in lower level directories recursively (so do not include them) or you can have a single non-recursive makefile formed by including other makefiles. I don't think it's possible to do both.

How to include (correctly) a makefile into an other makefile?

The only valid answer is "using the include directive" (other than the usual "it depends".) Any other advice depends on the structure of the makefiles. Makefiles designed to be included will obviously be easier to include. Just including some random makefile and hoping it will work ... won't work. Check out Implementing non-recursive make and Boilermake for how to make non-recursive makefiles work.

Note that there's no need to include them at the top of the file and doing so may be a bad idea, as the default target becomes the first one in the included file.

like image 92
Jonathan Wakely Avatar answered Oct 02 '22 15:10

Jonathan Wakely