Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advice on organizing sources and builds in C++ development under Linux (CMake as generator)

Tags:

c++

linux

cmake

Can someone propose some good practices for organizing your source files and managing builds when using C++ under Linux. I use CMake to manage my builds although i do not use complex constructs at this point of time. Let us say we have the following three scenarios.
1. For a makefile application to simply build a few executables from simple .cpp and .h files
2. For creating a static/shared library which uses other popular shared libraries say OpenCV and OpenGL, for example.
3. More complicated types, for example, let us say we need to create an executable whose source files use external libraries like OpenCV and also a custom static library that we have built ourselves(for example, a custom static library with associated headers that we built with step2 above).

I am sure many of you work on complicated library projects where the build process is not that simple. I am really looking forward to amazing answers from open source enthusiasts and hackers who contribute to open source projects. How do you guys organize your source code?

like image 713
hAcKnRoCk Avatar asked Feb 08 '11 16:02

hAcKnRoCk


Video Answer


1 Answers

Since you are using CMake I would suggest using out of source builds (Either completely outside or in a build subdirectory of the project root directory) When using more than one configuration and/or compiler at the same time you can create a separate build directory for each one.

In the CMakeLists.txt in the project root directory I set up stuff that is to be used by all CMakeLists.txt files in the src subdirectory. I put all the sources for the executables and libraries in a src subdirectory and usually I group the sources that form a single library or executable inside their own subdirectory within src together with an accompanying CMakeLists.txt that describes how to build it. I usually don't separate include files from the sources.

I also have a cmake subdirectory in the project root dir where i put files specific to CMake like find modules and in my case a special cmake module which fixed the paths that the Eclipse IDE autodiscovers.

|--cmake
|  |
|  |--FindXXX.cmake
|
|--src
|  |
|  |--projectABC
|  |  |
|  |  |--CMakeLists.txt
|  |
|  |--library1
|  |  |
|  |  |--CMakeLists.txt
|  |
|  |--library2
|     |
|     |--CMakeLists.txt
|
|--CMakeLists.txt
|
|--build-release
|--build-debug
|--build-msvc-release
|--[...]
like image 145
trenki Avatar answered Sep 21 '22 11:09

trenki