How to set up Doxygen and CMake for builds that generate headerfiles during the build process?(如何为在构建过程中生成头文件的构建设置DOOXO和CMake?)
问题描述
概述
我的CMake项目中的一些头文件是在构建过程中自动生成的。我的目标是设置CMakeLists.txt
,以便为我的项目构建文档也会触发头文件的生成(而不会触发任何其他构建)。基本的CMake/DO2设置遵循MS tutorial中的初始步骤。
详细信息
此项目的目录结构有些非常规:
${CMAKE_SOURCE_DIR}
是cmake/
保存顶级CMakeLists.txt
文件,${CMAKE_BINARY_DIR}
是cmake/cmake-build-debug
。
我希望生成的文档以cmake/cmake-build-debug/docs
结尾。但是,我想要提交到存储库的CMake和DO2配置使这些文件位于docs/
中。
源文件如下:
squawk.cpp
:#include "squawk.hpp" void squawk(const std::string &s) { std::cout<<STRFY(SQUAWK)<<": "<<s<<std::endl; }
squawk.hpp
:#include <iostream> #include <string> #define STRINGIFY(X) #X #define STRFY(X) STRINGIFY(X) #include "squawk.hd" void squawk(const std::string &s);
main.cpp
:#include "squawk.hpp" int main(int argc, char* argv[]) { const std::string s = "Hello World!"; squawk(s); return 0; }
squawk.def
:#define SQUAWK squeal
squawk.def
,将squeal
更改为caw
,并另存为squawk.hd
,这是由sed
执行的,说明在squawk.ss
:s/(#define SQUAWK)(.*)/1 caw/
中。这是头文件生成过程。即使我只构建文档而不必构建squawk
二进制文件,我也希望发生这种情况。
推荐答案
解决方案
解决此问题的关键是使用add_custom_target
而不是add_custom_command
生成头文件。
cmake/CMakeLists.txt
:### Set variables ### set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../src) set(SOURCES ${SRC_DIR}/squawk.cpp ${SRC_DIR}/main.cpp) set(SQUAWK_DEF ${SRC_DIR}/squawk.def) set(SQUAWK_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/squawk.ss) # Create include/ dir to save generated .hd file set(INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include) file(MAKE_DIRECTORY ${INCLUDE_DIR}) set(SQUAWK_HD ${INCLUDE_DIR}/squawk.hd) set(BIN_NAME squawk) ### Generate .hd file ### add_custom_target(gen_hd BYPRODUCTS ${SQUAWK_HD} DEPENDS ${SQUAWK_DEF} SOURCES ${SQUAWK_DEF} ${SQUAWK_SCRIPT} COMMAND sed --regexp-extended -f ${SQUAWK_SCRIPT} ${SQUAWK_DEF} >${SQUAWK_HD}) ### Add target ### add_executable(${BIN_NAME} ${SOURCES} ${SQUAWK_HD}) target_include_directories(${BIN_NAME} PUBLIC ${INCLUDE_DIR}) ### Build documentation ### if(DOCUMENTATION) add_subdirectory(../docs ${CMAKE_CURRENT_BINARY_DIR}/docs) endif()
docs/CMakeLists.txt
:### Find Doxygen ### # We need Doxygen 1.9.2 or higher find_package(Doxygen 1.9.2 REQUIRED) ### Find headers ### get_target_property(SQUAWK_PUBLIC_HEADER_DIR ${BIN_NAME} INTERFACE_INCLUDE_DIRECTORIES) set(SQUAWK_PUBLIC_HEADERS "") foreach(d ${SQUAWK_PUBLIC_HEADER_DIR}) file(GLOB_RECURSE SPH ${d}/*.h*) list(APPEND SQUAWK_PUBLIC_HEADERS ${SPH}) endforeach() ### Set Doxygen variables ### # Set variables which will be subsituted # in the Doxyfile.in for INPUT and OUTPUT set(DOXYGEN_INPUT_DIR "${SQUAWK_PUBLIC_HEADER_DIR}" "${SOURCES}") # Replace ; with space so string adheres to Doxygen syntax string(REPLACE ";" " " DOXYGEN_INPUT_DIR "${DOXYGEN_INPUT_DIR}") set(DOXYGEN_OUTPUT_DIR "${CMAKE_BINARY_DIR}/docs") ### Generate Doxyfile ### # Use docs/Doxyfile.in and configure_file # to create cmake/docs/Doxyfile which will be used # to generate the documentation set(DOXYFILE_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in) set(DOXYFILE_OUT ${DOXYGEN_OUTPUT_DIR}/Doxyfile) configure_file(${DOXYFILE_IN} ${DOXYFILE_OUT} @ONLY) ### Run Doxygen ### # Create directory where documentation will live # Specify index file which will be the output of the command # Add command and target file(MAKE_DIRECTORY ${DOXYGEN_OUTPUT_DIR}) set(DOXYGEN_INDEX_FILE ${DOXYGEN_OUTPUT_DIR}/html/index.html) add_custom_command(OUTPUT ${DOXYGEN_INDEX_FILE} COMMAND ${DOXYGEN_EXECUTABLE} Doxyfile MAIN_DEPENDENCY "${DOXYFILE_IN}" DEPENDS ${SQUAWK_PUBLIC_HEADERS} gen_hd WORKING_DIRECTORY ${DOXYGEN_OUTPUT_DIR} COMMENT "Generating doxygen docs") add_custom_target(doxydocs ALL DEPENDS ${DOXYGEN_INDEX_FILE} ${SQUAWK_HD} WORKING_DIRECTORY ${DOXYGEN_OUTPUT_DIR})
通过上述设置,构建doxydocs
将触发gen_hd
,从而生成squawk.hd
。如果我们生成squawk
,它将发出:
这篇关于如何为在构建过程中生成头文件的构建设置DOOXO和CMake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为在构建过程中生成头文件的构建设置DOOXO和CMake?
基础教程推荐
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04