# Use the OpenMP Libraries

This section describes the steps needed to set up and use the OpenMP Libraries from the command line. On Windows systems, you can also build applications compiled with the OpenMP libraries in the Microsoft Visual Studio development environment.

For a list of the options and libraries used by the OpenMP libraries, see OpenMP Support Libraries.

## Set Up Environment

Set up your environment for access to the compiler to ensure that the appropriate OpenMP library is available during linking.

Linux and macOS

On Linux and macOS systems you can source the appropriate script file (setvars file).

Windows

On Windows systems you can either execute the appropriate batch (.bat) file or use the command-line window supplied in the compiler program folder that already has the environment set up.

To use the `gfortran` compiler with the Intel® OpenMP library along with the OpenMP API functions:

1. Use the `use omp_lib` statement to compile the omp_lib.f90 source file, which is in the `include` directory.

2. Add the -I option to the compile command line with appropriate path to the directory containing the resulting module file.

During compilation, ensure that the version of omp_lib.h or omp_lib.mod used when compiling is the version provided by that compiler.

### CAUTION

Be aware that when using the GCC or Microsoft Compiler, you may inadvertently use inappropriate header or module files. To avoid this, copy the header or module file(s) to a separate directory and put it in the appropriate `include` path using the -I option.

If a program uses data structures or classes that contain members with data types defined in the omp_lib.h file, then source files that use those data structures should all be compiled with the same omp_lib.h file.

## Linux Examples

This section shows several examples of using OpenMP with the Intel Fortran Compiler Classic and Intel Fortran Compiler from the command line on Linux.

The command for the Intel® Fortran Compiler Classic is ifort. The command for the Intel® Fortran Compiler is ifx. In all Windows and Linux examples that follow, the command ifx can be substituted for the command ifort. macOS is only supported for ifort.

Compile and Link OpenMP Libraries

You can compile an application and link the Intel OpenMP libraries with a single command using the -qopenmp option. For example:

    ifx -qopenmp hello.f90

By default, the Intel Fortran Compiler Classic and Intel Fortran Compiler performs a dynamic link of the OpenMP libraries. To perform a static link (not recommended), add the option -qopenmp-link=static. The option -qopenmp-link controls whether the linker uses static or dynamic OpenMP libraries on Linux and macOS systems (default is -qopenmp-link=dynamic). See OpenMP Support Libraries for more information about dynamic and static OpenMP libraries.

Link OpenMP Object Files Compiled with GCC or Intel Fortran Compiler Classic and Intel Fortran Compiler

You can use the ifort/ifx compilers with the gcc/g++ compilers to compile parts of an application and create object files that can then be linked (object-level interoperability).

When using gcc or the g++ compiler to link the application with the Intel Fortran Compiler Classic and Intel Fortran Compiler OpenMP compatibility library, you need to specify the following:

- The Intel OpenMP library name using the -l option
- The Linux pthread library using the -l option
- The path to the Intel libraries where the Intel Fortran Compiler Classic and Intel Fortran Compiler is installed using the -L option

For example:

1. Compile foo.c and bar.c with gcc, using the -fopenmp option to enable OpenMP support:

       gcc -fopenmp -c foo.c bar.c

   The -c prevents linking at this step.
2. Use the gcc compiler to link the application object code with the Intel OpenMP library:

       gcc foo.o bar.o -liomp5 -lpthread -L<install_dir>/lib

   where <install_dir> is the location of the installed Intel OpenMP library.

Alternately, you can use the Intel Fortran Compiler Classic and Intel Fortran Compiler to link the application so that you don't need to specify the gcc-l option, -L option, and the -lpthread options.

For example:

1. Compile foo.c with gcc, using the gcc -fopenmp option to enable OpenMP:

       gcc -fopenmp -c foo.c

2. Compile bar.c with icx, using the -qopenmp option to enable OpenMP:

       icx -qopenmp -c bar.c

3. Use the ifort compiler to link the resulting application object code with the Intel OpenMP library:

       ifort -qopenmp -nofor-main foo.o bar.o

Link Mixed C/C++ and Fortran Object Files

You can mix C/C++ and Fortran object files and link the Intel OpenMP libraries using GNU, GCC, or Intel Fortran Compiler Classic and Intel Fortran Compiler compilers.

This example shows mixed C and Fortran sources, linked using the Intel Fortran Compiler Classic and Intel Fortran Compiler. Consider the mixed source files ibar.c, gbar.c, and foo.f:

1. Compile ibar.c using the icx compiler:

       icx -qopenmp -c ibar.c

2. Compile gbar.c using the gcc compiler:

       gcc -fopenmp -c gbar.c

3. Compile foo.f using the ifort compiler:

       ifort -qopenmp -c foo.f

4. Use the ifort compiler to link the resulting object files:

       ifort -qopenmp foo.o ibar.o gbar.o

When using ifort or ifx, if the main program does not exist in a Fortran object file that is compiled by either ifort or ifx, specify the -nofor-main option on the command line during linking.

### Note

Do not mix objects created by the Intel Fortran Compiler Classic and Intel Fortran Compiler with the GNU Fortran Compiler (gfortran); instead, recompile all Fortran sources with either ifort or ifx, or recompile all Fortran sources with the GNU Fortran Compiler . The GNU Fortran Compiler is only available on Linux operating systems.

When using the GNU gfortran Compiler to link the application with the Intel Fortran Compiler Classic and Intel Fortran Compiler OpenMP compatibility library, you need to specify the following:

- The Intel® OpenMP compatibility library name and the Intel® irc libraries using the -l option
- The Linux pthread library using the -l option
- The path to the Intel® libraries where the is installed using the -L option

You do not need to specify the -fopenmp option on the link line.

For example, consider the mixed source files ibar.c, gbar.c, and foo.f:

1. Compile ibar.c using the icx compiler:

       icx -qopenmp -c ibar.c

2. Compile gbar.c using the GCC compiler:

       gcc -fopenmp -c gbar.c

3. Compile foo.f using the gfortran compiler:

       gfortran -fopenmp -c foo.f

4. Use the gfortran compiler to link the application object code with the Intel OpenMP library. You do not need to specify the -fopenmp option in the link command:

       gfortran foo.o ibar.o gbar.o -lirc -liomp5 -lpthread -lc -L<install_dir>/lib

   where <install_dir> is the location of the installed Intel OpenMP library.

Alternately, you can use the Intel Fortran Compiler Classic and Intel Fortran Compiler . to link the application object code but need to pass multiple gfortran libraries using the -l options at the link step.

This example shows mixed C and GNU Fortran sources linked using the icx compiler. Consider the mixed source files ibar.c and foo.f:

1. Compile the C source with the icx compiler:

       icx -qopenmp -c ibar.c

2. Compile the GNU Fortran source with gfortran:

       gfortran -fopenmp -c foo.f

3. Use ifort to link the resulting object files with the -l option to pass the needed gfortran libraries:

       ifort -qopenmp -nofor-main foo.o ibar.o -lgfortran

## macOS Examples

This section shows several examples of using OpenMP with the Intel Fortran Compiler Classic and Intel Fortran Compiler from the command line on macOS.

macOS is only supported for ifort.

Compile and Link OpenMP Libraries

You can compile an application and link the Intel OpenMP libraries with a single command using the -qopenmp option. For example:

    ifort -qopenmp hello.f90

By default, the Intel Fortran Compiler Classic performs a dynamic link of the OpenMP libraries. To perform a static link (not recommended), add the option -qopenmp-link=static. The option -qopenmp-link controls whether the linker uses static or dynamic OpenMP libraries on Linux and macOS systems (default is -qopenmp-link=dynamic).

Link Mixed OpenMP Object Files

You can mix OpenMP object files compiled by the Intel C++ Compiler Classic and Intel Fortran Compiler Classic.

This example shows mixed C and Fortran sources, compiled by the icc and ifort drivers, linked using the Intel Fortran Compiler Classic . Consider the mixed source files ibar.c and foo.f90:

1. Compile ibar.c using icc:

       icc -qopenmp -c ibar.c

   The -c prevents linking at this step.
2. Compile foo.f90 using ifort:

       ifort -qopenmp -c foo.f90

3. Use ifort to link the resulting object files:

       ifort -qopenmp foo.o ibar.o

When using ifort, if the main program does not exist in a Fortran object file that is compiled by ifort, specify the -nofor-main option on the command line during linking.

## Windows Examples

This section shows several examples of using OpenMP with the Intel Fortran Compiler Classic and Intel Fortran Compiler from the command line on Windows.

Compile and Link OpenMP Libraries

You can compile an application and link the Compatibility libraries with a single command using the /Qopenmp option. By default, the Intel Fortran Compiler Classic and Intel Fortran Compiler performs a dynamic link of the OpenMP libraries.

For example, to compile source file hello.cpp and link Compatibility libraries using the Intel Fortran Compiler Classic and Intel Fortran Compiler:

    ifort /MD /Qopenmp hello.f90

Mix OpenMP Object Files Compiled with Visual C++ Compiler or Intel® Fortran Compiler Classic and Intel® Fortran Compiler

You can use the Intel® Fortran Compiler Classic and Intel® Fortran Compiler with the Visual C++ Compiler to compile parts of an application and create object files that can then be linked (object-level interoperability).

For example:

1. Compile f1.c and f2.c with the Visual C++ Compiler, using the /openmp option to enable OpenMP support:

       cl /MD /openmp /c f1.c f2.c

   The /c prevents linking at this step.
2. Compile f3.f90 and f4.f90 with the ifort compiler, using the /Qopenmp option to enable OpenMP support:

       ifort /MD /Qopenmp /c f3.f90 f4.f90

3. Use the ifort compiler to link the resulting application object code with the Intel C++ Compiler OpenMP library:

       ifort /MD /Qopenmp f1.obj f2.obj f3.obj f4.obj /Feapp /link /nodefaultlib:vcomp

   The /Fe specifies the generated executable file name.

Parent topic: OpenMP* Library Support

## See Also

qopenmp, Qopenmp compiler option
Using IPO
OpenMP Support Libraries
qopenmp-link, Qopenmp-link compiler option