3
votes

I have built clang 4.0 from source on Ubuntu 16.04 and am trying to compile a simple OpenMP program but receive the following errors.

/tmp/test-7f2c7c.o: In function `main':
/home/me/sf_shared/test.c:(.text+0x52): undefined reference to `__kmpc_fork_call'
/tmp/test-7f2c7c.o: In function `.omp_outlined.':
/home/me/sf_shared/test.c:(.text+0xd9): undefined reference to `__kmpc_for_static_init_4'
/home/me/sf_shared/test.c:(.text+0x16d): undefined reference to `__kmpc_for_static_fini'
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)

To compile I am using ./bin/clang ~/sf_shared/tset.c -fopenmp where bin is the bin folder where I have build clang from source and test.c is a simple openmp program.

Adding -v results in the following

clang version 4.0.1
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/me/release_build/./bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/home/me/release_build/bin/clang-4.0" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all
 -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model static
 -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables
 -fuse-init-array -target-cpu x86-64 -v -dwarf-column-info -debugger-tuning=gdb -resource-dir /home/me
 /release_build/bin/../lib/clang/4.0.1 -internal-isystem /usr/local/include -internal-isystem /home/me
 /release_build/bin/../lib/clang/4.0.1/include -internal-externc-isystem /usr/include/x86_64-linux-gnu
 -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /home/me
 /release_build -ferror-limit 19 -fmessage-length 117 -fopenmp -fobjc-runtime=gcc -fdiagnostics-show-option -o 
 tmp/test-c9b0bd.o -x c /home/me/sf_shared/test.c
clang -cc1 version 4.0.1 based upon LLVM 4.0.1 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /home/me/release_build/bin/../lib/clang/4.0.1/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
 "/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/
 ld-linux-x86-64.so.2 -o a.out /usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc
 /x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/5.4.0/crtbegin.o -L/usr/
 lib/gcc/x86_64-linux-gnu/5.4.0 -L/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu -L/lib/
 x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../.. -L/
 home/me/release_build/bin/../lib -L/lib -L/usr/lib /tmp/test-c9b0bd.o -lomp -lgcc --as-needed -lgcc_s 
 --no-as-needed -lpthread -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/5.4.0/
 crtend.o /usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crtn.o
/tmp/test-c9b0bd.o: In function `main':
/home/me/sf_shared/test.c:(.text+0x52): undefined reference to `__kmpc_fork_call'
/tmp/test-c9b0bd.o: In function `.omp_outlined.':
/home/me/sf_shared/test.c:(.text+0xd9): undefined reference to `__kmpc_for_static_init_4'
/home/me/sf_shared/test.c:(.text+0x15f): undefined reference to `__kmpc_for_static_fini'
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)

Any idea as to why the openmp sections are not being linked correctly?

I've include test.c below if anyone is curious

#include <omp.h>
#include <stdio.h>

int main() {
    printf("Max threads: %d\n", omp_get_max_threads());
    omp_set_dynamic(0);
    omp_set_num_threads(omp_get_max_threads());

    #pragma omp parallel for
    for (int i = 0; i < 32; ++i) {
        printf("I am thread %d\n", omp_get_thread_num());
    }
}
1
using -fopenmp=libiomp5 has resolved the issue, but what is libiomp5 and why does it not function correctly without it? Andrey Bokhanko's answer to the question you shared says "You need to use -fopenmp=libomp to enable OpenMP in clang. -fopenmp just links libgomp but ignores all the pragmas. Weird, I know -- and will be changed in the trunk soon", referring to clang 3.7. Has that still not been addressed in clang 4.0.1? - user8543704

1 Answers

2
votes

Installing the libiomp5 package and changing -fopenmp to -fopenmp=libiomp5 when compiling has resolved the issue.