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

问题描述 投票:0回答:1

我在其中一遍中定义了cl :: opt参数。

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

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

llvm llvm-ir
1个回答
0
投票

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

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

extern std::string input;

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

std::string input;
cl::opt<std::string> 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部分。

© www.soinside.com 2019 - 2024. All rights reserved.