How to instrument the code programmatically?

I am trying to instrument my code programmatically with the Clang CompilerInstance. According to the documentation, I should be able to setup the profiling level to IR by:

clang::CompilerInstance compiler = new clang::CompilerInstance();
auto& codeGenOpts = compiler.getCodeGenOpts();

However, when I try to compile the code by executing an action with the compiler object, I got the following error:

JIT session error: Symbols not found: [ __llvm_profile_instrument_target ]

I did some research and found that this symbol is defined in compiler-rt/lib/profile/InstrProfiling.h. In other words, this error is happening probably because the preprocessor doesn’t include this header file.

I wonder if I should include the header file manually to leverage the instrumentation? It’s a little weird for me to do so since I think the compilerInstance should handle this as we can do something like clang -c example.c -fprofile-generate -o example.o even example.c doesn’t include any profiling-related header file.