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.
https://cmake.org/cmake/help/latest/command/target_include_directories.html
https://cmake.org/cmake/help/latest/command/include_directories.html
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
A lot of the time, the include_directories(. . .) and add_executable(. . .) statements are already present. Typically, they will use some variable instead of a hard path; try to trace that variable and see where it is defined. Usually, it will lead to a file(GLOB . . .) statement, and this is where you can add the paths to any desired files.
Look at other CMake files which are known to be working. It’s likely that the thing you are trying to do has already been done before. Try to identify the right part, then copy-paste it in the right order with some refactoring.
Take a look at our CMake documentation, CMake within ZeroPilot, and see if it can give you some pointers. Additionally, search online resources for more information; Google is your best friend when developing software!
Most importantly, don’t be afraid to reach out to other members on the team for help. A 3 minute question can save you anywhere from 30 minutes to 3 hours of trying to weed out a problem.