Add_subdirectory Example

Jul 23, 2020 All variables produced by Kconfig have this prefix. So, for example, APPLICATIONWORKQUEUESTACKSIZE becomes CONFIGAPPLICATIONWORKQUEUESTACKSIZE. In the CMakeLists.txt above, you can see that a subdirectory is only added if the driver is “enabled”. CONFIGBH1749 happens to be the top-level environment variable to make that happen. Use addsubdirectory to add a subdirectory containing a CMakeLists.txt. You often want a cmake folder. See the Testing chapter for an example of adding a submodule.

Submodule method (preferred)

To use this method, just checkout GoogleTest as a submodule:1

Cmake Include Subdirectory

May 01, 2020 set(gtestdisablepthreads ON) addsubdirectory(googletest) That code example will only work reliably with CMake 3.13 or later, as it is currently written. In earlier CMake versions, the option command won’t consider non-cache variables for the initial value of gtestdisablepthreads where the cache variable does not exist yet. 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. To make use of the new library we will add an addsubdirectory call in the top-level CMakeLists.txt file so that the library will get built. We add the new library to the executable, and add MathFunctions as an include directory so that the mysqrt.h header file can be found.


Then, in your main CMakeLists.txt:

I would recommend using something like PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME to set the default for the PACKAGE_TESTS option, since this should only build by default if this is the current project.As mentioned before, you have to do the enable_testing in your main CMakeLists.

Now, in your tests directory:

If you did this in your main CMakeLists, you could use a normal add_subdirectory; the extra path here is needed to correct the build path because we are calling it from a subdirectory. Permaculture garden planner software.

The next line is optional, but keeps your CACHE cleaner:

If you are interested in keeping IDEs that support folders clean, I would also add these lines:

Then, to add a test, I'd recommend the following macro:

What Is A Subdirectory

This will allow you to quickly and simply add tests. Feel free to adjust to suit your needs. If you haven't seen it before, ARGN is 'every argument after the listed ones'.Modify the macro to meet your needs. For example, if you're testing libraries and need to link in different libraries for different tests, you might use this:

Download method

You can use the downloader in my CMake helper repository, using CMake's include command.


This is a downloader for GoogleTest, based on the excellent DownloadProject tool. Downloading a copy for each project is the recommended way to use GoogleTest (so much so, in fact, that they have disabled the automatic CMake install target), so this respects that design decision. This method downloads the project at configure time, so that IDEs correctly find the libraries. Using it is simple:


Note: add_gtest is just a macro that adds gtest, gmock, and gtest_main, and then runs add_test to create a test with the same name:

FetchContent: CMake 3.11

The example for the FetchContent module is GoogleTest:

1. Here I've assumed that you are working on a GitHub repository by using the relative path to googletest. ↩