The following information is biased. But in a good way, I think. I'm going to tell you how to structure the directories in your project. This is based on convention, but will help you:
- Easily read other projects following the same patterns,
- Avoid a pattern that causes conflicts,
- Keep from muddling and complicating your build.
First, this is what your files should look like when you start if your project is creatively called
project, with a library called
lib, and a executable called
The names are not absolute; you'll see contention about
tests/, and the application folder may be called something else (or not exist for a library-only project). You'll also sometime see a
python folder for python bindings, or a
cmake folder for helper CMake files, like
Find<library>.cmake files. But the basics are there.
The same process will walk through # the project's entire directory structure. Addsubdirectory (Hello) addsubdirectory (Demo) Then for each subdirectory specified, CMakeLists.txt files are created. In the./Hello directory, the following CMakeLists.txt file is created: # Create a library called 'Hello' which includes the source file 'hello.cxx'. You probably don't want a single flat project structure, especially if you have many files and targets. You'll also want to tag 'usage requirements' to libra.
Notice a few things already apparent; the
CMakeLists.txt files are split up over all source directories, and are not in the include directories. This is because you should be able to copy the contents of the include directory to
/usr/include or similar directly (except for configuration headers, which I go over in another chapter), and not have any extra files or cause any conflicts. That's also why there is a directory for your project inside the include directory. Use
add_subdirectory to add a subdirectory containing a
You often want a
cmake folder, with all of your helper modules. This is where your
Find*.cmake files go. An set of some common helpers is at github.com/CLIUtils/cmake. To add this folder to your CMake path:
Cmake Subdirectory Target
extern folder should contain git submodules almost exclusively. That way, you can control the version of the dependencies explicitly, but still upgrade easily. See the Testing chapter for an example of adding a submodule.
You should have something like
/build* in your
.gitignore, so that users can make build directories in the source directory and use those to build. A few packages prohibit this, but it's much better than doing a true out-of-source build and having to type something different for each package you build.
Cmake Subdirectory Option
If you want to avoid the build directory being in a valid source directory, add this near the top of your CMakeLists:
Cmake Subdirectory Targets
See the extended code example here.