Add_subdirectory Example

Add_subdirectory example in html
  1. Cmake Add Subdirectory Example
  2. Add_subdirectory Example In Html
  3. What Is A Subdirectory
  4. Cmake Add Subdirectory Absolute Path

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.
  • The following example demonstrates some key ideas of CMake. Make sure that you have CMake installed prior to running this example (go here for instructions). There are three directories involved. The top level directory has two subdirectories called./Demo and./Hello. In the directory./Hello, a library is built.
  • Mar 22, 2021 addsubdirectory; In modern CMake, adddependencies option is rarely used and hence I am not adding CMake adddependencies to the above list. Let us see how addsubdirectory is used to add a dependency. The best example I could find was CMake using itself to build.
  • Copy the contents of one of the existing example folders to this new folder. For example, copy the contents of examples/beaconing. In the examples/CMakeLists.txt file, depending on your platform (see Compatibility): If your example supports the nRF52810 or nRF52820 SoC, add the new folder as a new addsubdirectory command entry.

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 app:

First written on 2020-09-11.Last updated on 2021-01-02. In this article and several more, I will be discussing developing a very simple C library and application using CMake and Visual Studio Code. I will also use git and Google Test, and port the project from Windows to Linux. Most of the information is applicable to using.


The names are not absolute; you'll see contention about test/ vs. 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.


Cmake Add Subdirectory Example


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 CMakeLists.txt.

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 To add this folder to your CMake path:

Add_subdirectory Example In Html

Your 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.

What Is A Subdirectory

If you want to avoid the build directory being in a valid source directory, add this near the top of your CMakeLists:

Cmake Add Subdirectory Absolute Path

See the extended code example here.