[CMake] Linking programs with Clang & OpenMP fails

Kim Walisch kim.walisch at gmail.com
Fri May 4 18:17:39 EDT 2018


Hi,

I have figured out what the problem is:

LLVM/Clang with OpenMP requires linking against libatomic only when
using 128-bit integers. The following program fails to compile using
Clang-6.0 on Ubuntu 18 x86_64 using:

$ clang++ -fopenmp=libomp test.cpp

#include <omp.h>

int main() {
    __int128_t sum = 0;

    #pragma omp parallel for reduction(+: sum)
    for (int i = 0; i < 100; i++)
        sum += i;

    return 0;
}

But it compiles fine when adding -latomic:
clang++ -fopenmp=libomp test.cpp -latomic

For me, this is neither a CMake bug nor an LLVM/Clang bug but it is
still very confusing as GCC does not require linking against libatomic.

Regards,
Kim

On Fri, May 4, 2018 at 7:48 PM, Kim Walisch <kim.walisch at gmail.com> wrote:

> Hi,
>
> The latest Clang-6.0 compiler finally enables OpenMP by default on
> Linux (e.g. Ubuntu-18 x86_64).
>
> But OpenMP programs using Clang-6.0/CMake-3.10 fail to compile:
>
> [100%] Linking CXX executable primecount
> libprimecount.a(P2.cpp.o): In function `.omp_outlined..7':
> P2.cpp:(.text+0x2194): undefined reference to `__atomic_load'
> P2.cpp:(.text+0x21ef): undefined reference to `__atomic_compare_exchange'
> CMakeFiles/primecount.dir/build.make:148: recipe for target 'primecount'
> failed
> make[2]: *** [primecount] Error 1
>
> The problem is that:
>
> target_link_libraries(myprogram OpenMP::OpenMP_CXX)
>
> does not add libatomic to the linker options which LLVM/Clang's OpenMP
> library depends upon. When I manually add -latomic to the linker
> options my OpenMP programs compile & link fine using Clang-6.0/CMake-3.10.
>
> Regards,
> Kim
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180505/45c844dd/attachment-0001.html>


More information about the CMake mailing list