How can I build a C ++ project with several interdependent subdirectories?

I have a C ++ project where I used directories as a more organizational element - a way to use packages in Java or directories in PHP. Directories are not intended for self-contained elements, but just a way to organize the entire project and not allowing me to be overloaded with sources. How can I create my CMakeLists.txt files to handle this? Creating directory libraries does not seem to be suitable, as they are all interdependent and not intended to be used in this way.

As a related problem, most of the examples that I saw in several subdirectories in CMake (and there are not many of them) ignored or ignored the problem of setting include_directories , which is something I had problems with. If you do not comb my source files to determine which file depends on which directory and which directory, is it still easy to install all the directories in /src/ , since potential ones include directories and allow CMake to work, which ones really depend?

Here is an example structure:

 --src --top1 --mid1 --bot1 --src1.cpp --hdr1.h --bot2 --src2.cpp --hdr2.h --mid2 --bot3 --src3.cpp --src4.cpp --hdr3.h --top2 --mid3 --src5.cpp --hdr4.h 

So, etc. How can I structure CMakeLists.txt files to handle such a structure?

+58
c ++ cmake
Aug 03 2018-11-11T00:
source share
2 answers

Since the directory structure in your project is there to support your files, one approach is to have CMakeLists.txt , which automatically finds all the source files in the src directory, and also adds all the directories as included directories that have a header file in them. The following CMake file can serve as a starting point:

 cmake_minimum_required(VERSION 3.0) project (Foo) file(GLOB_RECURSE Foo_SOURCES "src/*.cpp") file(GLOB_RECURSE Foo_HEADERS "src/*.h") set (Foo_INCLUDE_DIRS "") foreach (_headerFile ${Foo_HEADERS}) get_filename_component(_dir ${_headerFile} PATH) list (APPEND Foo_INCLUDE_DIRS ${_dir}) endforeach() list(REMOVE_DUPLICATES Foo_INCLUDE_DIRS) add_executable (FooExe ${Foo_SOURCES}) target_include_directories(FooExe PRIVATE ${Foo_INCLUDE_DIRS}) 

Two file(GLOB_RECURSE ... commands file(GLOB_RECURSE ... define a set of source and header files. The foreach calculates the set of included directories from the list of all header files.

One of the drawbacks when calculating a set of source files is that CMake will not automatically detect when new files are added to the source tree. You need to manually create assembly files.

+50
Aug 03 2018-11-11T00:
source share

I am not an expert at CMake, but since there are no other answers, I will take a look at the document and let it know. Organizing the source and including files in different directories is pretty much the norm.

It looks like CMake allows you to specify a list of include directories: http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:include_directories

So something like:

 include_directories("src/top1/mid1/bot1" "src/top1/mid1/bot2/" ... ) 

They are passed to the compiler so that it can find the header files and passed for each of the source files. Therefore, any of your source files should be able to include any of the header files (which, I think, is what you are asking for).

Similarly, you should be able to list all the source files in the add_executable command :

 add_executable(name "src/top1/mid1/bot1/src1.cpp" "src/top1/id1/bot2/src2.cpp" ...) 

So, that would be a naive way to make everything build. Each source file will be compiled and will look for headers in all of these directories, and then the object files will be linked together. Consider whether there is a way to simplify this, so you donโ€™t need so many included folders, maybe there are only a few common header files that need to be referenced by all the source files. If things get complicated, you can create sub-tiers in libraries, etc. Also consider separating source files and headers (e.g. in src and include).

+2
Aug 03 '11 at 7:22
source share



All Articles