5

I am trying to run a simple project with OpenMP. Since Visual Studio only supports OpenMP 2 so I try to compile and run the project using LLVM clang-cl that comes with Visual Studio 2019. The compilation part seems to be ok, but in the link phase, the linker cannot resolve the OMP functions.

This is my code, there is only 1 file:

#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();
}

My Visual Studio project properties:

  • Windows SDK version: 10.0(latest installed version) (10.0.18362.0)
  • Platform toolset: LLVM (clang-cl)
  • C/c++ - Command Line - Additional Options: /Zc:twoPhase- -Xclang -fopenmp -v
  • Linker - Additional Dependencies: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\Llvm\lib\libomp.lib and C:\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

Error log when running the project

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.

I am using Visual Studio Community 2019. So how do I config the project in order to OpenMP work?


I have also tried to compile like in this answer and it works.

clang -fopenmp -o Source.obj -c Source.cpp
clang -fopenmp -o Source.exe Source.obj

It works for clang-cl too

clang-cl -Xclang -fopenmp -o Source.obj -c Source.cpp
clang-cl /clang:-fopenmp -o Source.exe Source.obj -v

But I don't know how to make Visual Studio build the project using the above way.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Dauto98
  • 177
  • 3
  • 12

2 Answers2

0

have you added the library to your project dependencies? enter image description here

Landstalker
  • 1,368
  • 8
  • 9
  • Yes, I already added the folder containing `libomp.lib` and `libiomp5md.lib`: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\Llvm\lib` – Dauto98 Feb 17 '20 at 15:52
  • I have not seen **include headers** of OpenMP in your program ?! how do you plan to use this library without including its headers ? – Landstalker Feb 17 '20 at 15:59
  • It still not work even with a header. Besides, I can compile and run in GitBash using `clang` even without header – Dauto98 Feb 17 '20 at 16:39
  • You don't need to include OpenMP headers for your program to require linking against the OpenMP runtime. All those `#pragma omp` translate to calls to functions inside `libomp`. – Hristo Iliev Apr 02 '20 at 20:17
0

You are probably linking with the incorrect openmp library. I guess the problem is you are building the x64 version, but linking with the x86 library. Note there are two directories: ...\VC\Tools\Llvm\lib ...\VC\Tools\Llvm\x64\lib