custom LLVM Pass with options fails to load

I'm working on an LLVM Pass plugin and I'm running into a problem when loading it into opt.
I want to have a custom option for my pass and added an llvm::cl::opt #include'ing "llvm/Support/CommandLine.h"

linking the dependant libs causes the following error when loading it with opt:
opt: CommandLine Error: Option 'debug-pass' registered more than once!

I narrowed it down to the Core lib but without it I get this

opt: /home/qwert/projects/ext/repos/llvm/6.0.0/lib/Support/CommandLine.cpp:281: void (anonymous namespace)::CommandLineParser::registerCategory(llvm::cl::OptionCategory *): Assertion `count_if(RegisteredOptionCategories, [cat](const OptionCategory *Category) { return cat->getName() == Category->getName(); }) == 0 && "Duplicate option categories"' failed.
LLVMSymbolizer: error reading file: No such file or directory
#0 0x0000000001d8eea4 (opt+0x1d8eea4)
#1 0x0000000001d8f206 (opt+0x1d8f206)
#2 0x00007fb283220390 __restore_rt (/lib/x86_64-linux-gnu/
#3 0x00007fb2821cd428 gsignal (/lib/x86_64-linux-gnu/
#4 0x00007fb2821cf02a abort (/lib/x86_64-linux-gnu/
#5 0x00007fb2821c5bd7 (/lib/x86_64-linux-gnu/
#6 0x00007fb2821c5c82 (/lib/x86_64-linux-gnu/
#7 0x0000000001d2b2a9 (opt+0x1d2b2a9)
#8 0x00007fb281efebc2 _GLOBAL__sub_I_CommandLine.cpp (/home/qwert/projects/ext/projects/cmake/llvm/6.0.0/lib/
#9 0x00007fb28343c6ba (/lib64/
#10 0x00007fb28343c7cb (/lib64/
#11 0x00007fb2834418e2 (/lib64/
#12 0x00007fb28343c564 (/lib64/
#13 0x00007fb283440da9 (/lib64/
#14 0x00007fb282e03f09 __asprintf (/lib/x86_64-linux-gnu/
#15 0x00007fb28343c564 (/lib64/
#16 0x00007fb282e04571 (/lib/x86_64-linux-gnu/
#17 0x00007fb282e03fa1 dlopen (/lib/x86_64-linux-gnu/
#18 0x0000000001d7b88b (opt+0x1d7b88b)
#19 0x0000000001d44de9 (opt+0x1d44de9)
#20 0x000000000069f0b4 (opt+0x69f0b4)
#21 0x0000000001d35a91 (opt+0x1d35a91)
#22 0x0000000001d2e3b9 (opt+0x1d2e3b9)
#23 0x00000000006958b4 (opt+0x6958b4)
#24 0x00007fb2821b8830 __libc_start_main (/lib/x86_64-linux-gnu/
#25 0x0000000000686be9 (opt+0x686be9)
Stack dump:
0. Program arguments: opt -load /home/maliusarth/projects/ext/projects/cmake/llvm/6.0.0/lib/ -testPass test.cpp

in my cmakelists.txt I have the following:


  Core # narrowed it down to this

add_llvm_loadable_module( TestPass


in my cpp I have this:

#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/CommandLine.h"

#include <string>

// Apply a custom category to all command-line options so that they are the
// only ones displayed.
static llvm::cl::OptionCategory testCategory("testPass Options");

static llvm::cl::opt<std::string>
  testOpt("testOpt", llvm::cl::desc("testOpt"),
       llvm::cl::value_desc("test pass opt"), llvm::cl::cat(testCategory));

namespace test
char TestPass::ID = 0;

static ::llvm::RegisterPass<test::TestPass>
  X("testPass", "test pass", false /* Only looks at CFG */,
    false /* Analysis Pass */);

the rest of the file is more or less straight out of the Hello example.

I'm working with this llvm version

I'd appreciate any help on this issue.


Hi Viktor,

I believe you shouldn’t set LLVM_LINK_COMPONENTS.

You don’t need to link your TestPass against anything since opt is expected to have everything loaded already. If you’re linking against a statically built LLVM, then this way you’ll pull in other definitions of cl::opt’ions, which’ll then clash with the ones that are in opt.


Hi Philip,

thanks for the quick answer.
That makes sense, but when leaving the set LLVM_LINK_COMPONENTS out
I get an undefined symbol when loading the plugin: _ZTVN4llvm2cl3optINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEELb0ENS0_6parserIS7_EEEE

which boils down to

llvm::cl::opt<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, false, llvm::cl::parser<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>


llvm::cl::opt<std::string, false, llvm::cl::parser<std::string>>

I guess...

any ideas?


Quoting Philip Pfaffe <>:

No quite, that’s the vtable for cl::opt<…>. Are you loading a library that’s built with RTTI in an LLVM linked without?


I’m building the plugin from inside the LLVM directory structure (LLVMBuild?).
I haven’t changed any compile options and looking into the the flags for my plugin files match the flags of other files with -fno-rtti being set.
So that’s not it, unfortunately.


Quoting Philip Pfaffe <>:

I don’t know your setup, but the error message is unambiguous. You’re linking/loading a binary with rtti into a context without.