如何在传递之间共享cl :: opt参数?

时间:2019-11-02 02:33:30

标签: llvm llvm-ir

我已经在一次通过中定义了cl :: opt参数。

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

我想知道如何与另一遍共享该参数?我试图将其移到头文件中,让另一遍通过它,但是报告了多个定义错误。

1 个答案:

答案 0 :(得分:2)

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

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

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变量中。现在,您可以从不同的TU访问input的值,但仍然只有一个定义。

请参见Internal and External storage部分。

相关问题