Specify OpenMP to GCC
In general, keep in mind that the directives and the functions are different things; the former are controlled by -fopenmp
and the latter are controlled by linking to the OpenMP library.
(Updated to incorporate comments) Try using the
-fopenmp
and-static
options 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 hello
The following command is probably compiling properly because it is linking to the shared object for OpenMP (
libgomp.so
). Note that the-static
option is not used.gcc hello.c -L/usr/lib/gcc/i686-linux-gnu/4.4/ -lgomp -o hello
If you don't specify the
-fopenmp
option, OpenMP directives should be ignored.
![Tim](https://i.stack.imgur.com/3PCjR.png?s=256&g=1)
Tim
Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.
Updated on January 27, 2020Comments
-
Tim over 4 years
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 ...),
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?
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?
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?
Thanks and regards!
-
Admin about 11 yearsFrom GCC documentation: -fopenmp flag also arranges for automatic linking of the OpenMP runtime library. See here: gcc.gnu.org/onlinedocs/libgomp/…
-
Tim about 13 yearsThanks! I am considering the code with API and without directives. (1) 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) when using -fopenmp, why explicit linking to libgomp.so is not required?
-
ejd about 13 years(1) You can statically link OpenMP by using "-fopenmp -static". What you will find is that when this is specified it uses "-lgomp -lrt". (2) If the system supports the dynamic linker then it is used and when you specify -fopenmp it will link with libgomp.so.
-
Royi over 6 yearsIt seems both the compiler and the linker requires the
-fopenmp
flag. -
ChrisZZ over 2 years@ejd Hi, I would like to know how can people know "-fopenmp -static" links to openmp and rt libraries. I do
man gcc
and search-fopenmp -static
but not found result, and search-fopenmp
not get result ofrt
library.