Reputation: 1899
My project consists of about a dozen components, and source code for each of these lives in its own subdirectory under the main source code directory. Many of the components use header files from other components, and I'm trying to arrange things so that these header files are all available in a single include/
directory. Stop me now if this is a bad idea or if there's a better way to make headers from one component easily available to another.
It's simple enough to copy a component's headers to include/
after the component is built: cp *.hpp ../include
in the makefile works well. But when make clean
is run on an individual component, I want to remove that component's headers from include/
without removing any others. As far as I can tell, this means I have to maintain a list of the component's header files in a "remove" command in the makefile, like this:
clean:
rm -fv ../include/header1.hpp ../include/header2.hpp ...
Maintaining this list is manual work, which I'd like to automate. I want to get this right, because I anticipate problems if a component which depends on the copied header gets built before a new version of the same header is copied into place - very time-consuming-to-diagnose problems.
How can I get my makefile to clean up a list of headers in a separate directory? Alternatively, is there a better way to make my header files available to other components in the same project?
Upvotes: 0
Views: 436
Reputation: 6866
Copying the files and using the copies to build other components is error prone no matter how you do it, manually or not. larsmans suggested some alternatives above.
But if you want to stay with the copy-on-build method, cleaning up through make can be done like this:
TO_COPY = $(wildcard *.hpp)
TO_REMOVE = $(patsubst %.hpp, ../include/%.hpp, $(TO_COPY))
The first variable will contain all .hpp files in the current directory, the second one will contain the same files with ../include/
prepended to each filename.
So you can copy the files to the include folder with:
cp $(TO_COPY) ../include/
and remove them at the end with:
rm $(TO_REMOVE)
Upvotes: 1
Reputation: 363507
Alternatively, is there a better way to make my header files available to other components in the same project?
There are two obvious and simple ways:
include
dir in the first placeCPPFLAGS
for each component to tell it where to find its dependencies.I'd recommend the former approach for small, tightly-coupled projects. The second approach is better for really componentized systems, as it forces you to think about the internal dependencies in your project.
Upvotes: 1