How to get cpp files from different directories compiled into one folder?

Viewed 2728

I have a number of C++ files distributed in several folders.

a_library/
    file1.cpp
    file2.cpp
    category1/
        file3.cpp
        file4.cpp

They are guaruanteed to be uniquely named. I want to compile all those C++ files to seperate Object-files in the obj/ directory.

I have a list of all source-files with relative path, and their corresponding destination-names.

a_library/file1.cpp
a_library/file2.cpp
a_library/category1/file3.cpp
a_library/category1/file4.cpp

obj/file1.obj
obj/file2.obj
obj/file3.obj
obj/file4.obj

How can I make a rule that will convert a C++ file from the first list to a object-file from the second one?

These attempts do not work:

obj/%.obj: %:cpp
    # ...

%.obj: %.cpp
    # ...

.cpp.obj:
    # ...

I would like to not write rules like this:

obj/%.obj: a_library/%.cpp
    # ...

obj/%.obj: a_library/category1/%.cpp
    # ...
5 Answers
Related