Adding Tests or Mocks to CMake

There’s no definite procedure for adding things to CMake, but there are two general rules: tell the compiler where to find header files and tell the compiler where to find source files.

 

Header Files

If we want to use a header file, then we need to provide CMake with the path to where it’s located. To do so, we use target_include_directories(. . .) or include_directories(. . .).

 

Here is an example:

target_include_directories(${PROJECT_NAME} <path_to_file>) include_directories(<path_to_file>)

 

For more information/direction on how to use these, see the following sources.

Source Files

Similarly if we want to compile a source file, we also must tell CMake where to find it. The command to do so is add_executable(. . .).

 

Another example:

add_executable(${PROJECT_NAME} <path_to_file>)

 

The CMake documentation for this function can be found below.

General Tips