简体   繁体   English

LLVM / Clang 8 Windows中OpenMP代码编译

[英]LLVM / Clang 8 Compilation of OpenMP Code in Windows

I'm using the Windows version of Clang (LLVM) 8 under Windows.我在 Windows 下使用Clang (LLVM) 8 的 Windows 版本。
I'm compiling a code which uses OpenMP .我正在编译使用OpenMP的代码。

Under the lib folder of Clang there are 2 files which are OpenMP related:在 Clang 的lib文件夹下有 2 个与 OpenMP 相关的文件:

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

My questions are:我的问题是:

  1. When I compile the code I use the flags -Xclang -fopenmp for the compiler.当我编译代码时,我为编译器使用标志-Xclang -fopenmp In in GCC and ICC using the flags tell the compiler to link the OpenMP library automatically.在 GCC 中,ICC 使用标志告诉编译器自动链接 OpenMP 库。 What about Clang? Clang 呢? Does it do it automatically or must I link with libomp.lib manually?它是自动执行还是我必须手动链接libomp.lib Is there a way to trigger automatic linking to the OpenMP library?有没有办法触发自动链接到 OpenMP 库?
    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 .回答迈克尔克莱姆在下面的回答中对此进行了回答——使用clang驱动程序进行编译和链接,然后-fopenmp将像在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 .当我手动链接libomp.lib (定义为链接器的库)时,output exe需要libomp.dll ,而提供的 OpenMP 动态库是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.回答libomp.dllbin文件夹中提供,而不是在lib文件夹中提供。
  3. What's the proper way to utilize OpenMP in Clang under Windows?在 Windows 下的 Clang 中使用 OpenMP 的正确方法是什么? The clang-cl driver doesn't work with /openmp or -openmp as the MSVC's cl compiler. clang-cl驱动程序不适用于/openmp-openmp作为 MSVC 的cl编译器。
    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 ).回答:目前可以使用clang -fopenmp...clang-cl -Xclang -fopenmp...clang-cl /clang:-fopenmp... (相当于-Xclang -fopenmp )来完成。

Remark评论
On Windows I use Windows Driver of Clang using clang-cl .在 Windows 上,我使用 Windows Driver of Clang using clang-cl

Adding clarity to what the OpenMP libraries actually are, and how to use them on Windows with clang-cl进一步说明 OpenMP 库的实际含义,以及如何使用clang-cl在 Windows 上使用它们

libomp.dll and libiomp5md.dll ARE THE SAME FILES! libomp.dll 和 libiomp5md.dll 是相同的文件!

When compiling for Windows, you link against libomp.lib OR libiomp5md.lib which will link to the same-named DLL at runtime, ie libomp.dll OR libiomp5md.dll respectively.为 Windows 编译时,您链接到libomp.liblibiomp5md.lib ,它们将在运行时链接到同名的 DLL,即分别libomp.dlllibiomp5md.dll

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.如果您加载 2 个使用“不同名称 DLL”的文件,解释器将崩溃并给您一个令人讨厌的错误,例如: OMP:错误 #15:初始化 libiomp5md.dll,但发现 libomp.dll 已经初始化。

Why?为什么? Because the program has no idea they are the same DLL, they have different names, so it assumes they are different.因为程序不知道它们是相同的 DLL,它们具有不同的名称,所以它假定它们是不同的。 And it crashes.它崩溃了。 For this reason only, you can choose to swap which OpenMP DLL you link to in your program.仅出于这个原因,您可以选择交换您在程序中链接到的 OpenMP DLL。

If your program doesn't crash and give you an error, you can keep using the same link to OpenMP.如果您的程序没有崩溃并给您一个错误,您可以继续使用相同的 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."如果使用clang-cl.exe ,它是 MSVC cl.exe的“插入式”Clang 替代品,您应该传递一个编译器参数,例如-Xclang -fopenmp ,它将把参数转换为“Clang 语言”。 Don't forget to still pass to the linker the OpenMP LIB you chose, because on Windows, it won't be automatic.不要忘记仍然将您选择的 OpenMP LIB 传递给 linker,因为在 Windows 上,它不会是自动的。

That's all I've learned as brief as possible about OpenMP linking on Windows.这就是我在 Windows 上尽可能简要地了解 OpenMP 链接的全部内容。

To compile and link OpenMP code with clang on Windows, you will have to pass -fopenmp to both the compiler and the linker:要在 Windows 上使用 clang 编译和链接 OpenMP 代码,您必须将-fopenmp传递给编译器和链接器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM