Intel® C++ Compiler Classic Developer Guide and Reference

ID 767249
Date 7/13/2023
Public
Document Table of Contents

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

During compilation, ensure that the version of omp.h used when compiling is the version provided by that compiler. For example, use the omp.h provided with GCC when you compile with GCC.

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.h file, then source files that use those data structures should all be compiled with the same omp.h file.

Linux Examples

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

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:

icpc -qopenmp hello.cpp

By default, the Intel C++ 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). See OpenMP Support Libraries for more information about dynamic and static OpenMP libraries.

Link OpenMP Object Files Compiled with GCC or Intel C++ Compiler Classic

You can use the icc/icpc 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 C++ Compiler Classic 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 C++ Compiler Classic 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 C++ Compiler Classic 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 icc, using the -qopenmp option to enable OpenMP:
    icc -qopenmp -c bar.c
  3. Use the icc compiler to link the resulting application object code with the Intel OpenMP library:
    icc -qopenmp 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 C++ Compiler Classic compilers.

This example shows mixed C and Fortran sources, linked using the Intel C++ Compiler Classic. Consider the mixed source files ibar.c, gbar.c, and foo.f, where the main program is contained in ibar.c:

  1. Compile ibar.c using the icc compiler:
    icc -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 icc compiler to link the resulting object files:
    icc -qopenmp foo.o ibar.o gbar.o

If the main program were contained in the Fortran file foo.f, the linking step must be performed by the ifort compiler.

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 C++ Compiler Classic 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 Intel C++ Compiler Classic Classic 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 icc compiler:
    icc -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 C++ Compiler Classic . 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 icc compiler. Consider the mixed source files ibar.c and foo.f:

  1. Compile the C source with the icc compiler:
    icc -qopenmp -c ibar.c
  2. Compile the GNU Fortran source with gfortran:
    gfortran -fopenmp -c foo.f
  3. Use icc to link the resulting object files with the -l option to pass the needed gfortran libraries:
    icc -qopenmp foo.o ibar.o -lgfortran

macOS Examples

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

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:

icpc -qopenmp hello.cpp

By default, the the Intel C++ 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 the Intel C++ 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 icc to link the resulting object files:
    icc -qopenmp foo.o ibar.o

Windows Examples

This section shows several examples of using OpenMP with the Intel C++ Compiler Classic 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 C++ Compiler Classic performs a dynamic link of the OpenMP libraries.

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

icl /MD /Qopenmp hello.cpp

When using the Microsoft Visual C++ Compiler, you should link with the Intel® OpenMP compatibility library. You need to avoid linking the Microsoft OpenMP runtime library (vcomp) and explicitly pass the name of the Intel® OpenMP compatibility library as linker options using the /link option. For example:

cl /MD /openmp hello.cpp /link /nodefaultlib:vcomp libiomp5md.lib

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

You can use the Intel C++ Compiler Classic 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.c and f4.c with the icl compiler, using the /Qopenmp option to enable OpenMP support:
    icl /MD /Qopenmp /c f3.c f4.c
  3. Use the icl compiler to link the resulting application object code with the Intel C++ Compiler OpenMP library:
    icl /MD /Qopenmp f1.obj f2.obj f3.obj f4.obj /Feapp /link /nodefaultlib:vcomp
    The /Fe specifies the generated executable file name.

    Alternatively, use the Visual C++ linker to link the application object code with the Compatibility library libiomp5md.lib:

    link f1.obj f2.obj f3.obj f4.obj /out:app.exe /nodefaultlib:vcomp libiomp5md.lib

Use Intel OpenMP Libraries from Visual Studio

When running Windows, you can make certain changes in the Visual C++ Visual Studio development environment to use the Intel C++ Compiler Classic and Visual C++ to create applications that use the Intel OpenMP libraries.

Set the project Property Pages to indicate the Intel OpenMP runtime library location:

  1. Open the project's property pages in from the main menu: Project > Properties (or right-click the Project name and select Properties) .

  2. Select Configuration Properties > Linker > General > Additional Library Directories.

  3. Enter the path to the Intel®-provided compiler libraries. For example, for an IA-32 architecture system (C/C++ only), enter:

    <Intel_compiler_installation_path>\windows\compiler\lib\ia32 win

Make the Intel OpenMP dynamic runtime library accessible at runtime; you must specify the corresponding path:

  1. Open the project's property pages in from the main menu: Project > Properties (or right-click the Project name and select Properties).

  2. Select Configuration Properties > Debugging > Environment.

  3. Enter the path to the Intel®-provided compiler libraries. For example, for an IA-32 architecture system (C/C++ only), enter:

    PATH=%PATH%;<Intel_compiler_installation_path>\windows\redist\ia32 win\compiler

Add the Intel OpenMP runtime library name to the linker options and exclude the default Microsoft OpenMP runtime library:

  1. Open the project's property pages in from the main menu: Project > Properties (or right-click the Project name and select Properties).

  2. Select Configuration Properties > Linker > Command Line > Additional Options.

  3. Enter the OpenMP library name and the Visual C++ linker option, /nodefaultlib:vcomp libiomp5md.lib.