c - 使用 clang 编译时 openmp 无法正确链接

标签 c clang openmp llvm-clang

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

/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是我从源代码构建clang的bin文件夹,test.c是一个简单的openmp程序。

添加 -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 部分没有正确链接吗?

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

#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 已解决问题。

关于c - 使用 clang 编译时 openmp 无法正确链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45983643/

相关文章:

c - 我怎样才能延迟 Linux 中断处理程序(我知道 sleep 通常是不可能的)

c - "fasttop"是什么意思?

c - malloc 和 calloc 在使用上的区别

docker - 在 Rocker/verse 上使用 rstan 构建 Docker 镜像

macos - 如何在 osx 上安装 clang++?

c++ - OpenMP omp_get_num_threads()V.S. omp_get_max_threads()

openmp - OpenMP 4.0 中 "simd"构造和 "for simd"构造的区别

c - 整数溢出不一致

c++ - Xcode 4.3 和 C++11 包含路径

c++ - OpenMP 并行化