32

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 ...),

  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?

Thanks and regards!

Tim
  • 1
  • 141
  • 372
  • 590
  • From GCC documentation: -fopenmp flag also arranges for automatic linking of the OpenMP runtime library. See here: http://gcc.gnu.org/onlinedocs/libgomp/Enabling-OpenMP.html#Enabling-OpenMP –  May 26 '13 at 15:17

1 Answers1

31

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.

  1. (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 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 -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.

Chris Frederick
  • 5,482
  • 3
  • 36
  • 44
  • Thanks! 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? – Tim Jun 15 '11 at 17:16
  • 3
    (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. – ejd Jun 15 '11 at 19:33
  • It seems both the compiler and the linker requires the `-fopenmp` flag. – Royi Feb 21 '18 at 00:34
  • @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 of `rt` library. – ChrisZZ Jan 13 '22 at 15:08