首页 > 解决方案 > 如何在传递之间共享 cl::opt 参数?

问题描述

我在其中一个通行证中定义了一个 cl::opt 参数。

cl::opt<std::string> input("input", cl::init(""), cl::desc("the input file"), cl::value_desc("the input file"));

我想知道如何与另一个传递共享此参数?我试图将它移动到一个头文件并让另一个传递包含它,但它报告了一个多重定义错误。

标签: llvmllvm-ir

解决方案


您需要将选项的存储设为外部。

在一些共享头文件中声明一个变量:

extern std::string input;

并仅在您的一个来源中定义它和选项本身:

std::string input;
cl::opt<std::string, true> inputFlag("input", cl::init(""), cl::desc("the input file"), cl::value_desc("the input file"), cl::location(input));

注意添加的cl::location(input)参数,它指示cl::opt将选项值存储在input变量中。现在您可以input从不同的 TU 访问 s 值,但仍然只有一个定义。

请参阅内部和外部存储部分。


推荐阅读