C语言 将 OpenMP 指定为 GCC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6351961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:54:42  来源:igfitidea点击:

Specify OpenMP to GCC

cgccopenmp

提问by Tim

For OpenMP, when my code is using the functions in its API (for example, omp_get_thread_num()) without using its directives (such as those #pragma omp ...),

对于 OpenMP,当我的代码使用其 API 中的函数(例如,omp_get_thread_num())而不使用其指令(例如那些 #pragma omp ...)时,

  1. why directly specifying libgomp.a to gcc instead of using -fopenmp doesn't work, such as

    gcc hello.c /usr/lib/gcc/i686-linux-gnu/4.4/libgomp.a  -o hello
    

    Update:I just found that linking to libgomp.a does not work, but linking to libgomp.so works. Does it mean OpenMP can not be static linked?

  2. Why -fopenmp only works without specifying the library files

    gcc hello.c -fopenmp -o hello
    

    Update:In other words, when using -fopenmp, why explicit linking to libgomp.so is not required?

  3. Why does this also compile:

    gcc hello.c -L/usr/lib/gcc/i686-linux-gnu/4.4/ -lgomp -o hello
    

    Will this ignore OpenMP directives in the code if there is any?

  1. 为什么直接将 libgomp.a 指定为 gcc 而不是使用 -fopenmp 不起作用,例如

    gcc hello.c /usr/lib/gcc/i686-linux-gnu/4.4/libgomp.a  -o hello
    

    更新:我刚刚发现链接到 libgomp.a 不起作用,但链接到 libgomp.so 起作用。这是否意味着 OpenMP 不能静态链接?

  2. 为什么 -fopenmp 只能在不指定库文件的情况下工作

    gcc hello.c -fopenmp -o hello
    

    更新:换句话说,当使用 -fopenmp 时,为什么不需要显式链接到 libgomp.so?

  3. 为什么这也编译:

    gcc hello.c -L/usr/lib/gcc/i686-linux-gnu/4.4/ -lgomp -o hello
    

    如果有的话,这会忽略代码中的 OpenMP 指令吗?

Thanks and regards!

感谢致敬!

回答by Chris Frederick

In general, keep in mind that the directives and the functions are different things; the former are controlled by -fopenmpand the latter are controlled by linking to the OpenMP library.

一般来说,请记住指令和函数是不同的东西;前者受控制,-fopenmp后者受链接到 OpenMP 库控制。

  1. (Updated to incorporate comments) Try using the -fopenmpand -staticoptions to statically link OpenMP. Because this implies -lgomp -lrt, the following command won't compile correctly unless you also specify the location of librt.a.

    gcc hello.c /usr/lib/gcc/i686-linux-gnu/4.4/libgomp.a  -o hello
    
  2. (Updated to incorporate comments) I imagine that the following command is compiling correctly because the OpenMP library is already in your library path and your system's dynamic linker is automatically linking with libgomp.so.

    gcc hello.c -fopenmp -o hello
    
  3. The following command is probably compiling properly because it is linking to the shared object for OpenMP (libgomp.so). Note that the -staticoption is notused.

    gcc hello.c -L/usr/lib/gcc/i686-linux-gnu/4.4/ -lgomp -o hello
    

    If you don't specify the -fopenmpoption, OpenMP directives should be ignored.

  1. 更新以包含评论)尝试使用-fopenmp-static选项来静态链接 OpenMP。因为这意味着-lgomp -lrt,除非您还指定了 的位置,否则以下命令将无法正确编译librt.a

    gcc hello.c /usr/lib/gcc/i686-linux-gnu/4.4/libgomp.a  -o hello
    
  2. 更新以包含注释)我认为以下命令编译正确,因为 OpenMP 库已经在您的库路径中,并且您系统的动态链接器会自动与libgomp.so.

    gcc hello.c -fopenmp -o hello
    
  3. 以下命令可能编译正确,因为它链接到 OpenMP ( libgomp.so)的共享对象。请注意,使用该-static选项。

    gcc hello.c -L/usr/lib/gcc/i686-linux-gnu/4.4/ -lgomp -o hello
    

    如果未指定该-fopenmp选项,则应忽略 OpenMP 指令。