LLVM CommandLine: how to reset arguments?

2019-09-18 20:21发布

I have two static libs that use LLVM command line to parse arguments:

// lib1
void main(int argc, const char **argv) {

  cl::opt<bool>LibOption1( ... ) // arg1
  cl::ParseCommandLineOptions(argc, argv, "lib1\n");
}

.

// lib2
void main(int argc, const char **argv) {

  // need to reset arguments list here ..

  cl::opt<bool>LibOption2( ... ) // arg2
  cl::ParseCommandLineOptions(argc, argv, "lib2\n"); // crash here!
}

The app is linked against two this libs and they parse arguments just fine if having only 1 lib in the app and crash while parsing arguments if having both libs in the app.

It seems that in lines with // arg argument is added in some global static list (?) of arguments and this makes them have mixed arguments list and affect to each other.

Is there any opportunity to reset that global list before declaring arguments?

PS. I've found that static arguments list in CommandLine.cpp:

/// RegisteredOptionList - This is the list of the command line options that
/// have statically constructed themselves.
static Option *RegisteredOptionList = 0;

1条回答
聊天终结者
2楼-- · 2019-09-18 20:41

What is void main supposed to be? main returns int, and in any case libraries can't provide a main. If this is in some namespace, still please don't write void main.

The global list is supposed to be a feature, so that each library can provide its own (differently-namespaced) arguments. The library should not be calling cl::ParseCommandLineOptions itself; only the true main function should do that. Additionally, in a library it doesn't make sense for cl::opt to be a local variable, because then it only exists for the duration of that function. (If you are the application, then you can arrange for cl::ParseCommandLineOptions to be called before the cl::opts expire).

查看更多
登录 后发表回答