views:

122

answers:

3

Hello!

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!

+1  A: 

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.

dimba
I was thinking without specifying what are dependicies, so cmake is my choice... But, I was searching on the internet, before asking this question, about how to make this ( since I'm totally new in cmake ), but could find anything... Could you help me, please ? :)
mfolnovich
Unfortunately the documentation of not very good, not enough examples. I advise you just to try to make small demo to investigate it. From my experience I know for example, that when I come to compile directory creating executable, cmake compiles first other directories containing libraries the binary should compile first
dimba
A: 

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.

Yuval F
I need this because I'm making projects where users should be able to add their codes, and I want to avoid editing Makefile...
mfolnovich
A: 

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.

Christopher Bruns