使用 clang-cl 在 Visual Studio 2019 中使用 Openmp 4/5

Dau*_*o98 5 openmp clang visual-c++ visual-studio-2019

我正在尝试使用 OpenMP 运行一个简单的项目。由于Visual Studio仅支持OpenMP 2,因此我尝试使用Visual Studio 2019自带的LLVM clang-cl编译并运行该项目。编译部分似乎没问题,但在链接阶段,链接器无法解析OMP函数。

这是我的代码,只有 1 个文件:

#include <stdio.h>

void fn() {
    #pragma omp parallel num_threads(5)
    {
        int i;
        #pragma omp task depend(in : i)
        for (i = 0; i < 1; i++) {
            printf("task\n");
        }
    }
}

int main() {
    printf("hello\n");
    fn();
}
Run Code Online (Sandbox Code Playgroud)

我的 Visual Studio 项目属性:

  • Windows SDK version:10.0(最新安装版本)(10.0.18362.0)
  • Platform toolset: LLVM (clang-cl)
  • C/c++ - Command Line - Additional Options:/Zc:twoPhase- -Xclang -fopenmp -v
  • Linker - Additional DependenciesC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\Llvm\lib\libomp.libC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\Llvm\lib\libiomp5md.lib
  • Linker - Additional Library Directories:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\Llvm\lib
  • Linker - Command Lines - Additional Options:-fopenmp -verbose

项目运行时的错误日志

1>lld-link : error : undefined symbol: __kmpc_global_thread_num
1>>>> referenced by D:\Repositories\Test-clang\Test-clang\Source.cpp:4
1>>>>               x64\Debug\Source.obj:(void __cdecl fn(void))
1>
1>lld-link : error : undefined symbol: __kmpc_push_num_threads
1>>>> referenced by D:\Repositories\Test-clang\Test-clang\Source.cpp:7
1>>>>               x64\Debug\Source.obj:(void __cdecl fn(void))
1>
1>lld-link : error : undefined symbol: __kmpc_fork_call
1>>>> referenced by D:\Repositories\Test-clang\Test-clang\Source.cpp:7
1>>>>               x64\Debug\Source.obj:(void __cdecl fn(void))
1>
1>lld-link : error : undefined symbol: __kmpc_omp_task_alloc
1>>>> referenced by D:\Repositories\Test-clang\Test-clang\Source.cpp:10
1>>>>               x64\Debug\Source.obj:(.omp_outlined._debug__)
1>
1>lld-link : error : undefined symbol: __kmpc_omp_task_with_deps
1>>>> referenced by D:\Repositories\Test-clang\Test-clang\Source.cpp:10
1>>>>               x64\Debug\Source.obj:(.omp_outlined._debug__)
1>Done building project "Test-clang.vcxproj" -- FAILED.
Run Code Online (Sandbox Code Playgroud)

我正在使用 Visual Studio Community 2019。那么如何配置项目才能使 OpenMP 工作?


我也尝试像这个答案一样进行编译并且它有效。

clang -fopenmp -o Source.obj -c Source.cpp
clang -fopenmp -o Source.exe Source.obj
Run Code Online (Sandbox Code Playgroud)

它也适用于 clang-cl

clang-cl -Xclang -fopenmp -o Source.obj -c Source.cpp
clang-cl /clang:-fopenmp -o Source.exe Source.obj -v
Run Code Online (Sandbox Code Playgroud)

但我不知道如何让 Visual Studio 使用上述方式构建项目。

Lan*_*ker 0

您是否已将库添加到项目依赖项中? 在此输入图像描述