LLVM CommandLine: how to reset arguments?

删除回忆录丶 提交于 2019-12-13 04:41:55

问题


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:


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).



来源:https://stackoverflow.com/questions/26078018/llvm-commandline-how-to-reset-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!