Skip to content

Patching llvm config.cpp to fix a bug

valtron edited this page Dec 23, 2012 · 2 revisions

See this bug and thread.

Once you get LLVM built, you typically use llvm-config with various options to compile a project that uses llvm. For example, g++ -g -O3 toy.cpp `llvm-config --cppflags --ldflags --libs core` -o toy.

--libs prints out LLVM libs. --ldflags prints out some linker flags as well as some system libs (like -lpthread -lm, among others). However, you'll get linker errors if the system libs are specified before the LLVM libs. Simply specifying --ldflags after --libs won't work.

To fix this so you can still use llvm-config, you'll need to patch main in llvm-src/tools/llvm-config/llvm-config.cpp.

  • Near the beginning of main: after bool PrintLibs = false, PrintLibNames = false, PrintLibFiles = false;, add bool PrintLDFlags = false;
  • Lower down in the loop that checks the -- options: on the line OS << "-L" << ActiveLibDir << ' ' << LLVM_SYSTEM_LIBS << ' ' << LLVM_LDFLAGS << '\n'; remove << ' ' << LLVM_SYSTEM_LIBS and add PrintLDFlags = true; on the next line
  • Near the bottom of main: before OS << '\n'; add
if (PrintLDFlags) {
  OS << ' ' << LLVM_SYSTEM_LIBS;
}
  • Right after OS << '\n'; and before } else if (!Components.empty()) {, add
} else if (PrintLDFlags) {
  OS << ' ' << LLVM_SYSTEM_LIBS << '\n';
Clone this wiki locally