mfolnovich
mfolnovich

Reputation: 217

(c)make - resursive compile

Let's assume I have directories like:

dir1
    main.cpp
    dir2
        abc.cpp
    dir3
        def.cpp
        dir4
            ghi.cpp
            jkl.cpp

And let's assume that main.cpp includes dir2/abc.cpp and dir3/def.cpp, def.cpp includes dir4/ghi.cpp and dir4/jkl.cpp.

My question is, how can I have one Makefile/CMakeLists.txt in dir1/ which goes in each directory recursively and compiles *.cpp, and then "joins" them?

Sorry for my english, hope that I explained my question well!

Thanks!

Upvotes: 3

Views: 237

Answers (3)

Christopher Bruns
Christopher Bruns

Reputation: 9498

One CMakeLists.txt file:

file(GLOB_RECURSE MAIN_SOURCES "dir1/*.cpp")

add_executable(MainExecutable ${MAIN_SOURCES})
# or
add_library(MyLibrary ${MAIN_SOURCES})

I am unsure what you mean by "joining" the sources. I am assuming here that you are combining them into either a library or an executable.

Upvotes: 1

Yuval F
Yuval F

Reputation: 20621

There is an open argument whether recursive make is a good thing. Please see this blog entry for a survey of relatively up-to-date pro and con papers.

Upvotes: 0

dimba
dimba

Reputation: 27571

For makefile, dir1/Makefile should:

  • declare that main.o dependends on dir2/abc.o and dir3/def.o
  • declare how to create dir2/abc.o and dir3/def.o

As for cmake it detects such dependencies "automatically" (binary depended on dir2/abc.o and dir3/def.o), so virually you don't need care about it.

Upvotes: 1

Related Questions