[Driver][OpenMP] -L for build/install dir is after system dirs


I’m trying to understand clang’s ordering of -L flags, which, on my system, leads to linking against a system libomp rather than the clang/LLVM libomp.

Specifically, on my system, running with -v shows that it specifies -L/usr/lib/x86_64-linux-gnu before the -L for my llvm/clang build/install directory. The effect is that it finds the system libomp.so.5 as libomp. When the executable loads, it then looks for libomp.so.5 instead of the libomp.so built for llvm, so even LD_LIBRARY_PATH doesn’t help it find the latter.

Is this intended behavior for some reason? Shouldn’t clang prefer its own libraries before system libraries?

A fix for my use case is to, in lib/Driver/ToolChains/Linux.cpp, move the following lines from near the end of the Linux constructor to earlier, right after the declaration of the Paths variable:

if (StringRef(D.Dir).startswith(SysRoot))
addPathIfExists(D, D.Dir + “/…/lib”, Paths);

By the way, in case it helps, the ToolChain constructor also tries to insert the following subdirectories of the build/install directory, but they don’t exist on my system:




Hi Joel,

I started a discussion about this on cfe-dev a while ago: http://lists.llvm.org/pipermail/cfe-dev/2017-January/052499.html
The short answer is that there are / might be more libraries than just the OpenMP runtime (or libunwind in my case), so changing the default might break on some systems.

I had a patch in https://reviews.llvm.org/D26244 that added a CMake flag to change the behaviour, but I lost interest after some time. Feel free to revive it if it's important for you.


Hi Jonas,