Dan S
Dan S

Reputation: 131

clang 3.8+ -fopenmp on linux: ld cannot find -lomp

I have installed clang 3.8 from the base repositories for both Debian Jessie and Fedora 24. When I try to compile a simple HelloWorld.cpp test program with clang++, and i pass the -fopenmp flag, in both cases i get the same error:

/usr/bin/ld: cannot find -lomp clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)

I see that if I instead pass -fopenmp=libgomp, it works. However, the Clang OpenMP website says that the OpenMP runtime is shipped with Clang 3.8. Why, then, can it not find the default libomp library? I do not see this library anywhere on my system.

Upvotes: 13

Views: 9441

Answers (3)

catleeball
catleeball

Reputation: 871

TL;DR

If you have libomp.so for llvm in somewhere like /usr/lib/llvm-12/lib make file /etc/ld.so.conf.d/libomp.conf with the line /usr/lib/llvm-12/lib in it, then run sudo ldconfig.



Intro

In my case, I had libomp-12-dev installed, but it was not in my linker's library path. See the footnote on how I found the library. There are a couple solutions in this scenario:


Add library path with ldconfig

If you want this in your default library path, consider using ldconfig [man page].

This will look for files in /etc/ld.so.conf. For me, running Ubuntu 20.04, this file only points to including files in the directory /etc/ld.so.conf.d.

$ cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf

As such, I made a config llvm-libomp-12 in my /etc/ld.so.conf.d directory that looks like this:

$ cat /etc/ld.so.conf.d/libomp.conf
# libomp.so for llvm
/usr/lib/llvm-12/lib

Then I asked ldconfig to update the paths with sudo ldconfig. You can add the -v flag and it will print all libraries and paths it is aware of.


Add library to environment variable

We can also direct the linker to our library using the $LD_LIBRARY_PATH environment variable

This may be advantageous if you're on a multiuser system and don't want to impact others, or if you have temporary changes to your library paths you would like to make in your shell.

See what your current $LD_LIBRARY_PATH is with echo $LD_LIBRARY_PATH. You may not have this set by default. Add paths to this variable, each delimited by a colon.

For your current shell session, simply append or prepend to your $LD_LIBRARY_PATH like this (assuming bash, zsh, or fish >v3.0):

export "$LD_LIBRARY_PATH:/path/to/lib"

Or for a more permanent change limited to your user, add the above export to your shell's config file (e.g. ~/.bashrc).


Manually specify library path(s) in compiler flags

Nice for a one-off specific library that you don't always want in your default library paths. Specify the path to the library as a flag like this:

-L/path/to/lib

For example:

clang++ -L/usr/lib/llvm-12/lib [...]

make -L/usr/lib/llvm-12/lib


Footnotes

On searching

If you don't know where a given library you need is, you can use things like find. Personally though, I used a package called mlocate that indexes files on my machine and allows you to search them.

  • Installing mlocate
    • sudo apt install mlocate
  • Updating the indexes
    • sudo updatedb
  • Searching for a substring
    • locate libomp.so

When I searched for where my libomp libraries were, I did this:

$ locate libomp.so
/usr/lib/llvm-12/lib/libomp.so
/usr/lib/llvm-12/lib/libomp.so.5
/usr/lib/x86_64-linux-gnu/libomp.so.5

Notably it seemed like clang was using the libomp.so.5 in the linux-gnu directory, but I needed it to be using the llvm library.

Environment used in this post

$ lsb_release --all
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.2 LTS
Release:    20.04
Codename:   focal

$ uname -a
Linux bip 5.8.0-48-generic #54~20.04.1-Ubuntu SMP Sat Mar 20 13:40:25 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

TODO

Some notes that could be added to this question:

  • Confirm and list priority of env vars vs config files vs flags (does this vary between compilers and linkers?)
  • Ordering library paths when using multiple config files (can we prefix with numbers to ensure the order libraries are parsed?)

Upvotes: 1

Michael M.
Michael M.

Reputation: 2584

There is good chances that the OpenMP development package is missing on your system.
On Ubuntu: sudo apt install libomp-dev

Upvotes: 10

user9478968
user9478968

Reputation:

If you have libomp installed correctly you will need to use -fopenmp=libomp. libgomp is for gcc. You might check that clang isn't symbollically linked to gcc on your computer.

Upvotes: 0

Related Questions