[CMake] Create main and sub-projects; be able to compile them together and individually.

🐋 Jan Hegewald jan.hegewald at awi.de
Tue Mar 8 05:02:36 EST 2016


Hi Muhammad,

> On 08.03.2016, at 06:17, Muhammad Osama <osama94 at gmail.com> wrote:
> 
> Hi, I am new to cmake and really hope am doing this correctly. I asked stackoverflow but didn't get a good enough answer for my specific problem here;
> 
> If I want root/sub-directories/ as separate sub-projects that can be compiled using the individualCMakeLists.txts in their folders I find myself literally copy pasting almost the entire root file CMakeLists.txt per sub-directory.
> 
> I was wondering if there is a better way to have a main project and then sub-projects that get the shared dependencies from main project and can be compiled without cmake-ing the root CMakeLists.txt. My directory structure is;
> 
> CMakeLists.txt (root project)
> | __ sub_dir-1
> | __ | __ CMakeLists.txt (sub-project)
> | __ sub_dir-2
> | __ | __ CMakeLists.txt (sub-project)
> | __ sub_dir-3
> | __ | __ CMakeLists.txt (sub-project)

I basically have the same project structure as you describe. I am also not sure what the best practice is here, but this is what I currently do:
I set all dependencies where they are required: right in the local CMakeLists.txt, i.e. sub_dir-1/CMakeLists.txt. Then "export" all required include/define/compiler flags dependencies via INTERFACE or PUBLIC flags of the various target_*** cmake functions, as appropriate. The sub-projects are added via add_subdirectory in cmake.
This way I can build each CMakeLists.txt individually if needed but still have everything DRYish.

HTH,
Jan



More information about the CMake mailing list