用clang编译时openmp不能正确链接 [英] openmp not linking correctly when compiling with clang

查看:712
本文介绍了用clang编译时openmp不能正确链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Ubuntu 16.04上从源代码构建了clang 4.0,并试图编译一个简单的OpenMP程序,但收到以下错误.

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)

要进行编译,我使用的是./bin/clang ~/sf_shared/tset.c -fopenmp,其中bin是bin文件夹,我在其中从源代码构建了clang,而test是一个简单的openmp程序.

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.

添加-v会导致以下结果

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)

关于为何未正确链接openmp部分的任何想法?

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

如果有人好奇,我会在下面包含test.c

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

推荐答案

安装libiomp5软件包,并在编译时将-fopenmp更改为-fopenmp=libiomp5即可解决问题.

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

这篇关于用clang编译时openmp不能正确链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆