Thursday, 15 May 2014

Modular C++ Project Build with CMake -



Modular C++ Project Build with CMake -

i'm trying find way build big modular c++ project cmake.

the construction of project following:

--project_root --src --folder_1 --source_1.h --source_1.cc --test_source_1.cc // file containing main unit tests --folder_2 --source_2.h --source_2.cc --test_source_2.cc // file containing main unit tests --folder_3 ...

and on.

each folder represent project module , each module might depend on other modules, illustration source_1.h may include source_2.h. every module folder may contains test file whole project have multiple executables.

how can build whole project cmake? how should write cmakelists.txt file?

thank lot.

there many, many examples out there of how construction cmake projects c++, many of referenced tutorial @user2485710 suggested in comment, i'm not going go super in-depth here, i'll @ to the lowest degree give starting point based on way want lay out folder structure.

the nice thing cmake can tree-decent using add_subdirectory command. lets split our cmake code required @ specific directory level. in otherwords, each cmakelists.txt file should minimal amount of work needed set things @ current depth in directory tree. in example, cmake tree might this:

--project_root --src --cmakelists.txt --folder_1 --cmakelists.txt --source_1.h --source_1.cc --test_source_1.cc // file containing main unit tests --folder_2 --cmakelists.txt --source_2.h --source_2.cc --test_source_2.cc // file containing main unit tests ...

in src/cmakelists.txt of project-level initialization, i.e. find_package, setting include-path, etc. add together next @ end:

add_subdirectory(folder_1) add_subdirectory(folder_2) ...

this tells cmake should in folders additional stuff do. in src/folder_1/cmakelists.txt, actual work of whatever combination of add_executable , add_library need build source_1.cc , test_source_1.cc, , likewise in src/folder_2/cmakelists.txt source_2.cc, etc.

the other nice thing cmake variables set higher tree propagated downwards through add_subdirectory. so, example, in src/cmakelists.txt can check sort of 'build unit-test' flag , set cmake variable there, , have in other cmakelists.txt files check variable. can super useful if have project cmake dynamically generating header files based on checking environment variables path-names , like.

c++ cmake

No comments:

Post a Comment