Royi
Royi

Reputation: 4963

LLVM / Clang 8 Compilation of OpenMP Code in Windows

I'm using the Windows version of Clang (LLVM) 8 under Windows.
I'm compiling a code which uses OpenMP.

Under the lib folder of Clang there are 2 files which are OpenMP related:

  1. libomp.lib.
  2. libiomp5md.dll.

My questions are:

  1. When I compile the code I use the flags -Xclang -fopenmp for the compiler. In in GCC and ICC using the flags tell the compiler to link the OpenMP library automatically. What about Clang? Does it do it automatically or must I link with libomp.lib manually? Is there a way to trigger automatic linking to the OpenMP library?
    Answer: This was answered in Michael Klemm's answer below - Use the clang driver both for compiling and linking and then the -fopenmp will work as in GCC.
  2. When I link with libomp.lib manually (Defining as a library for the linker) the output exe requires libomp.dll while the supplied OpenMP Dynamic Library is libiomp5md.dll. Is that a bug or is it because I link manually?
    Answer: The libomp.dll is supplied in the bin folder and not the lib folder.
  3. What's the proper way to utilize OpenMP in Clang under Windows? The clang-cl driver doesn't work with /openmp or -openmp as the MSVC's cl compiler.
    Answer: Currently it can be done either with clang -fopenmp ..., clang-cl -Xclang -fopenmp ... or clang-cl /clang:-fopenmp ... (Which is equivalent of -Xclang -fopenmp).

Remark
On Windows I use Windows Driver of Clang using clang-cl.

Upvotes: 4

Views: 2442

Answers (2)

Matt
Matt

Reputation: 2832

Adding clarity to what the OpenMP libraries actually are, and how to use them on Windows with clang-cl

libomp.dll and libiomp5md.dll ARE THE SAME FILES!

When compiling for Windows, you link against libomp.lib OR libiomp5md.lib which will link to the same-named DLL at runtime, i.e. libomp.dll OR libiomp5md.dll respectively.

If you load 2 files that use the "different-name DLL," the interpreter will crash and give you a nasty error like: OMP: Error #15: Initializing libiomp5md.dll, but found libomp.dll already initialized.

Why? Because the program has no idea they are the same DLL, they have different names, so it assumes they are different. And it crashes. For this reason only, you can choose to swap which OpenMP DLL you link to in your program.

If your program doesn't crash and give you an error, you can keep using the same link to OpenMP. Otherwise, to silence the error, link to the one that is loaded by another program already.

If using clang-cl.exe which is the "drop-in" Clang replacement for MSVC cl.exe you should pass a compiler argument such as -Xclang -fopenmp which will convert the argument over to "Clang language." Don't forget to still pass to the linker the OpenMP LIB you chose, because on Windows, it won't be automatic.

That's all I've learned as brief as possible about OpenMP linking on Windows.

Upvotes: 2

Michael Klemm
Michael Klemm

Reputation: 2873

To compile and link OpenMP code with clang on Windows, you will have to pass -fopenmp to both the compiler and the linker:

clang -fopenmp -o bla.obj -c bla.c
clang -fopenmp -o bla.exe bla.obj

Upvotes: 1

Related Questions