OpenMP linking errors on Windows using Ninja and clang

In our project we use OpenMP successfully in many systems, including Windows with VS compiler (and I believe both VS generator and Ninja). Relevant lines are essentially

target_link_libraries(buildblock OpenMP::OpenMP_CXX)
add_executable(bla buildblock)

However, I am now trying to use Ninja on AppVeyor which seems to then select CLang as compiler. Should work…

Unfortunately, I get linking errors like this

lld-link: error: undefined symbol: __kmpc_dispatch_init_8
...
lld-link: error: undefined symbol: omp_get_num_threads

Cutting various ugly bits in the log shows that no openmp flag nor library is added for linking:

C:\PROGRA~1\LLVM\bin\CLANG_~1.EXE -fuse-ld=lld-link -nostartfiles -nostdlib -Wno-undefined-var-template -O3 -DNDEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrt   src/utilities/CMakeFiles/list_detector_and_bin_info.dir/list_detector_and_bin_info.cxx.obj <cut> 
 src/buildblock/buildblock.lib  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames"

More details and full log file are at
OpenMP linking errors on Windows using Ninja and clang · Issue #1129 · UCL/STIR (github.com)

I did a search on the CMake issues and found an issue where the fopenmp linker flag is mentioned:
OpenMP with clang links to system libomp.so or drops rpath, when clang in non-standard location. (#21787) · Issues · CMake / CMake · GitLab (kitware.com)

Is this a CMake bug, or a bug in our code somewhere (I should probably add the openmp dependency on more of our libraries)? If the former, is there a workaround?

thanks for your help!
kris

It’s probably an issue with the FindOpenMP module. It probably just hasn’t had Clang on Windows support considered properly.

There is a problem with FindOpenMP not being able to find the libraries used at link time when the -fopenmp=libomp is given to the clang/clang++ driver. On linux boxes it generally use the -v flags to dump all the implicit libraries (including openmp). This trick seems broken on Windows.

You can use something like (or more target oriented) that to workaround the issue:
-DCMAKE_EXE_LINKER_FLAGS="-fopenmp"