I have defined a cl::opt parameter in one of my pass.
cl::opt<std::string> input("input", cl::init(""), cl::desc("the input file"), cl::value_desc("the input file"));
I wonder that how to share this parameter with another pass? I have tried to move it to a header file and let the another pass include it, but it reported a multiple definition error.
You need to make an option's storage external.
In some shared header file declare a variable:
extern std::string input;
And define it and the command line option itself in only one of your sources:
std::string input;
static cl::opt<std::string, true> inputFlag("input", cl::init(""), cl::desc("the input file"), cl::value_desc("the input file"), cl::location(input));
Note added cl::location(input)
argument, which instructs cl::opt
to store the command line option's value in the input
variable. Now you can access input
s value from different TUs, but still have only one definition.
Note that the variable that captures the command line option (inputFlag
) is a global variable in the TU in which it is defined in. It is a good idea to give it internal linkage via the static keyword.
See Internal and External storage section.