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
Specify OpenMP to GCC
提问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 ...)时,
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 helloUpdate: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?
Why -fopenmp only works without specifying the library files
gcc hello.c -fopenmp -o helloUpdate:In other words, when using -fopenmp, why explicit linking to libgomp.so is not required?
Why does this also compile:
gcc hello.c -L/usr/lib/gcc/i686-linux-gnu/4.4/ -lgomp -o helloWill this ignore OpenMP directives in the code if there is any?
为什么直接将 libgomp.a 指定为 gcc 而不是使用 -fopenmp 不起作用,例如
gcc hello.c /usr/lib/gcc/i686-linux-gnu/4.4/libgomp.a -o hello更新:我刚刚发现链接到 libgomp.a 不起作用,但链接到 libgomp.so 起作用。这是否意味着 OpenMP 不能静态链接?
为什么 -fopenmp 只能在不指定库文件的情况下工作
gcc hello.c -fopenmp -o hello更新:换句话说,当使用 -fopenmp 时,为什么不需要显式链接到 libgomp.so?
为什么这也编译:
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 库控制。
(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 oflibrt.a.gcc hello.c /usr/lib/gcc/i686-linux-gnu/4.4/libgomp.a -o hello(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 helloThe 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 helloIf you don't specify the
-fopenmpoption, OpenMP directives should be ignored.
(更新以包含评论)尝试使用
-fopenmp和-static选项来静态链接 OpenMP。因为这意味着-lgomp -lrt,除非您还指定了 的位置,否则以下命令将无法正确编译librt.a。gcc hello.c /usr/lib/gcc/i686-linux-gnu/4.4/libgomp.a -o hello(更新以包含注释)我认为以下命令编译正确,因为 OpenMP 库已经在您的库路径中,并且您系统的动态链接器会自动与
libgomp.so.gcc hello.c -fopenmp -o hello以下命令可能编译正确,因为它链接到 OpenMP (
libgomp.so)的共享对象。请注意,未使用该-static选项。gcc hello.c -L/usr/lib/gcc/i686-linux-gnu/4.4/ -lgomp -o hello如果未指定该
-fopenmp选项,则应忽略 OpenMP 指令。

