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.

4
  • libiomp5md.lib is definitely useless here as it is binary equivalent to libomp.lib. As well as -fopenmp for linker. Commented Jun 21, 2020 at 10:03
  • I've checked, the program is compiling and running flawlessly in VS 16.6.2 now. Commented Jun 21, 2020 at 10:13
  • Ah, forgot that you should switch off the error on multi-threading in Microsoft.Cpp.ClangCl.Common.targets like this <!-- <VCMessage Code="MSB8055" Type="Error" Condition="('%(ClCompile.OpenMPSupport)' == 'true')" /> -->. Commented Jun 21, 2020 at 10:23
  • 1
    The answer I posted at stackoverflow.com/a/68378031/7268445 may help you / some future reader out. Commented Jul 14, 2021 at 12:24

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
I have not seen include headers of OpenMP in your program ?! how do you plan to use this library without including its headers ?
It still not work even with a <omp.h> header. Besides, I can compile and run in GitBash using clang even without <omp.h> header
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.