我在其中一遍中定义了cl :: opt参数。
cl::opt<std::string> input("input", cl::init(""), cl::desc("the input file"), cl::value_desc("the input file"));
我想知道如何与另一遍共享该参数?我试图将其移到头文件中,并让另一遍通过它,但是报告了多个定义错误。
您需要将选项的存储空间设置为外部。
在某些共享头文件中声明一个变量:
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
的值,但仍然只有一个定义。