Visual Studio 2019 LLVM 中的 OpenMP 链接错误 [英] OpenMP linking errors in Visual Studio 2019 LLVM

查看:40
本文介绍了Visual Studio 2019 LLVM 中的 OpenMP 链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用集成到 Visual Studio 2019 的 LLVM 编译器构建 OpenMP 示例程序.

LLVM 编译器是从

我尝试了不同的选项:-fopenmp-Xclang -fopenmp 编译器标志,传递 -openmp-fopenmp 作为链接器标志,将 x64 更改为 x86,将 C:\Program Files\LLVM\lib 添加到 PATH.到目前为止没有任何帮助.

有趣的是,我可以通过从 x64 Native Tools Command Prompt for VS 2019 运行以下命令,从命令行构建相同的代码:

clang-cl -openmp llvmtest.cpp

所以,基本上,-openmp 标志就足够了......有没有人知道如何让它在 Visual Studio 中工作?

解决方案

最后,我设法让它工作了(感谢 LLVM 工具链扩展的作者 Marek Aniola,他帮助了我).

首先,我将 /openmp 标志传递给 LLVM -> 附加编译器选项(它适用于 -openmp也是).

其次,事实证明,从 Visual Studio 构建时 OpenMP 不会自动链接(与通过 clang-cl 从命令行编译相反).LLVM 的链接器设置的指定方式与 Microsoft 链接器的指定方式相同.

库的路径 C:\Program Files\LLVM\lib 必须添加到 Linker -> General -> 其他图书馆目录.

还需要在Linker -> Input -> Additional Dependencies 中列出libomp.lib.

链接器 -> 优化 -> 链接时间代码生成必须设置为默认.

这些步骤让我成功构建了代码(64 位应用程序).虽然 64 位 LLVM 编译器可以构建 32 位程序,但它只包含 x64 版本的 OpenMP 库,因此为了构建 32 位应用程序,我必须链接 libomp.lib 并复制libomp.dll 来自另一个 LLVM 发行版.如果有更好的方法来做到这一点,请告诉我.

I've been trying to build an OpenMP sample program with an LLVM compiler integrated to Visual Studio 2019.

LLVM compiler was downloaded from here (version 10.0, win64), C:\Program Files\LLVM\bin added to the PATH environment variable. LLVM Compiler Toolchain extension was installed from Visual Studio Marketplace.

It successfully builds a hello world program, but when I try to use OpenMP the linker fails with the following errors:

1>clang version 10.0.0
1>Target: x86_64-pc-windows-msvc
1>Thread model: posix
1>InstalledDir: C:\Program Files\LLVM\bin
1> (in-process)
1> "C:\\Program Files\\LLVM\\bin\\clang-cl.exe" -cc1 -triple x86_64-pc-windows-msvc19.26.28805 -emit-obj -mrelax-all -mincremental-linker-compatible -disable-free -disable-llvm-verifier -discard-value-names -main-file-name llvmtest.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -mllvm -x86-asm-syntax=intel -D_DEBUG -D_MT -D_DLL --dependent-lib=msvcrtd --dependent-lib=oldnames -stack-protector 2 -fcxx-exceptions -fexceptions -fexternc-nounwind -fms-volatile -fdefault-calling-conv=cdecl -fdiagnostics-format msvc -gcodeview -debug-info-kind=limited -v -resource-dir "C:\\Program Files\\LLVM\\lib\\clang\\10.0.0" -D _DEBUG -D _CONSOLE -D _UNICODE -D UNICODE -internal-isystem "C:\\Program Files\\LLVM\\lib\\clang\\10.0.0\\include" -internal-isystem "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.26.28801\\include" -internal-isystem "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.26.28801\\atlmfc\\include" -internal-isystem "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\VS\\include" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\ucrt" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\um" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\shared" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\winrt" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\cppwinrt" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\NETFXSDK\\4.6.1\\Include\\um" -O0 -Wall -Wno-error -fdeprecated-macro -fdebug-compilation-dir "Z:\\llvmtest" -ferror-limit 19 -fmessage-length 0 -fopenmp -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.26.28805 -std=c++14 -fdelayed-template-parsing -fobjc-runtime=gcc -fno-caret-diagnostics -fdiagnostics-show-option -faddrsig -o "Debug\\llvmtest.obj" -x c++ llvmtest.cpp
1>clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target x86_64-pc-windows-msvc
1>#include "..." search starts here:
1>#include <...> search starts here:
1> C:\Program Files\LLVM\lib\clang\10.0.0\include
1> C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include
1> C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\atlmfc\include
1> C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\VS\include
1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt
1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um
1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\shared
1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\winrt
1> C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt
1> C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\Include\um
1>End of search list.
1>lld-link : error : undefined symbol: __kmpc_global_thread_num
1>>>> referenced by Z:\llvmtest\llvmtest.cpp:9
1>>>>               Debug\llvmtest.obj:(main)
1>
1>lld-link : error : undefined symbol: __kmpc_push_num_threads
1>>>> referenced by Z:\llvmtest\llvmtest.cpp:15
1>>>>               Debug\llvmtest.obj:(main)
1>
1>lld-link : error : undefined symbol: __kmpc_fork_call
1>>>> referenced by Z:\llvmtest\llvmtest.cpp:15
1>>>>               Debug\llvmtest.obj:(main)
1>
1>lld-link : error : undefined symbol: omp_get_thread_num
1>>>> referenced by Z:\llvmtest\llvmtest.cpp:19
1>>>>               Debug\llvmtest.obj:(.omp_outlined._debug__)
1>
1>lld-link : error : undefined symbol: omp_get_num_threads
1>>>> referenced by Z:\llvmtest\llvmtest.cpp:25
1>>>>               Debug\llvmtest.obj:(.omp_outlined._debug__)
1>Done building project "llvmtest.vcxproj" -- FAILED.

The code is rather trivial:

    #include <omp.h>
    #include <stdio.h>

    int main()
    {
        int nthreads, tid;

        //omp_set_num_threads(4);   

    #pragma omp parallel private(tid) num_threads(3)
        {               
            tid = omp_get_thread_num();
            printf("Hello World from thread = %d\n", tid);

            if (tid == 0)
            {
                nthreads = omp_get_num_threads();
                printf("Number of threads = %d\n", nthreads);
            }

        }  /* All threads join master thread and terminate */
    }

Project settings shown below:

I have tried different options: -fopenmp, -Xclang -fopenmp compiler flags, passing -openmp and -fopenmp as linker flags, changing x64 to x86, adding C:\Program Files\LLVM\lib to the PATH. Nothing helped so far.

Interestingly enough, I can build the same code from the command line by running the following from x64 Native Tools Command Prompt for VS 2019:

clang-cl -openmp llvmtest.cpp

So, basically, -openmp flag was enough... Does anyone have an idea how to make it work in Visual Studio?

解决方案

Finally, I have managed to make it work (thanks to Marek Aniola, the author of the LLVM Toolchain extension, who helped me out).

First, I passed the /openmp flag to LLVM -> Additional Compiler Options (it works with -openmp too).

Second, it turns out that OpenMP is not automatically linked when building from Visual Studio (in contrast to compiling from the command line by means of clang-cl). Linker settings for LLVM are specified the same way as for the Microsoft linker.

The path to libraries C:\Program Files\LLVM\lib has to be added to Linker -> General -> Additional Library Directories.

It is also necessary to list libomp.lib in Linker -> Input -> Additional Dependencies.

Linker -> Optimization -> Link Time Code Generation must be set to Default.

These steps let me successfully build the code (64-bit application). Although the 64-bit LLVM compiler can build a 32-bit program, it includes only x64 version of OpenMP libraries, so in order to build a 32-bit application I had to link libomp.lib and copy libomp.dll from another LLVM distribution. If there is a better way to do that, please let me know.

这篇关于Visual Studio 2019 LLVM 中的 OpenMP 链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆