How to check if all project headers are standalone with CMake?

I want to automatically check if all the headers in the project can be built independently. This is a common technique to check if headers contain all their dependencies. Unfortunately, I could not find how this could be achieved. Maybe someone can help? As a newbie to CMake, I'm not sure I can develop a solution myself.

We hope that the solution will not require the creation of any new .cc files or the launch of any external scripts.

+4
source share
1 answer

Take a look at the standard features of the CMake module CheckCXXSourceCompiles and CheckCSourceCompiles . Both functions check whether the given (embedded) source code compiles correctly and if the links are correct. To check if the header is self-sufficient, the source code should consist of an include statement, which includes a header file for checking and the main function:

 include (CheckCXXSourceCompiles) set (CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}") check_cxx_source_compiles( "#include \"file.h\" int main() { return 0;}" File_H_IsSelfContained) message ("File_H_IsSelfContained: ${File_H_IsSelfContained}") 

Both check_cxx_source_compiles and check_c_source_compiles can only work on CMake setup time, which is probably not the way you want.

Since both functions use the basic CMake try_compile , which is not scriptable, it is not possible to use the functions in the generated CMake script, which runs as a custom target at build time.

+1
source

Source: https://habr.com/ru/post/1432476/


All Articles