首页 > 解决方案 > 在 sphinx 文档中提取 C++ 代码片段

问题描述

我正在尝试从我的源代码中提取特定的代码片段(测试)以将其输出到文档中。

TEST_CASE("std::vector") {
//! [MyTest]
  std::vector<int> v = {42,43};
  CHECK( v.size() == 2 );
//! [MyTest]
}

在 doxygen 中,我可以通过\snippet命令引用它。

现在,我想将代码片段中的代码直接显示到 sphinx 文档中

You can create a std::vector just like this:

INSERT THE CODE BETWEEN [MyTest] MARKERS HERE

The std::vector class automatically allocates memory for you

我怎么做?doxygensnippet我在 Breathe中没有找到任何指令。

注意:我不介意 sphinx 直接使用任何类型的标签为我提取代码,我不需要符合 doxygen。

标签: c++python-sphinxdoxygen

解决方案


感谢@Steve_Piercy,我能够找到我想要的:将literalinclude指令与start-afterand一起使用end-before

TEST_CASE("std::vector") {
//! [MyTest begin]
  std::vector<int> v = {42,43};
  CHECK( v.size() == 2 );
//! [MyTest end]
}

文档文件:

You can create a std::vector just like this:

.. literalinclude::  my_test_file.cpp
  :start-after: MyTest begin
  :end-before: MyTest end

The std::vector class automatically allocates memory for you

推荐阅读