Visible to Intel only — GUID: GUID-0E85F327-A19E-410C-8203-0A89C8F32635
Visible to Intel only — GUID: GUID-0E85F327-A19E-410C-8203-0A89C8F32635
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
On Linux 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.
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.
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® oneAPI DPC++/C++ Compiler 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:
icpx -qopenmp hello.cpp
By default, the Intel® oneAPI DPC++/C++ 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 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® oneAPI DPC++/C++ Compiler
You can use the icx/icpx 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 oneAPI DPC++/C++ 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 oneAPI DPC++/C++ Compiler is installed using the -L option
For example:
- 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. - 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 oneAPI DPC++/C++ Compiler to link the application so that you don't need to specify the gcc -liomp5 option, -L option, and the -lpthread options.
For example:
- Compile foo.c with gcc, using the gcc -fopenmp option to enable OpenMP:
gcc -fopenmp -c foo.c
- Compile bar.c with icx, using the -qopenmp option to enable OpenMP:
icx -qopenmp -c bar.c
- Use the icx compiler to link the resulting application object code with the Intel OpenMP library:
icx -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 oneAPI DPC++/C++ Compiler compilers.
This example shows mixed C and Fortran sources, linked using the Intel oneAPI DPC++/C++ Compiler. Consider the mixed source files ibar.c, gbar.c, and foo.f, where the main program is contained in ibar.c:
- Compile ibar.c using the icx compiler:
icx -qopenmp -c ibar.c
- Compile gbar.c using the gcc compiler:
gcc -fopenmp -c gbar.c
- Compile foo.f using the ifx compiler:
ifx -qopenmp -c foo.f
- Use the icx compiler to link the resulting object files:
icx -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.
Do not mix objects created by the with the GNU Fortran Compiler (gfortran); instead, recompile all Fortran sources with 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 oneAPI DPC++/C++ 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 Intel oneAPI DPC++/C++ Compiler is installed using the -L option
For example, consider the mixed source files ibar.c, gbar.c, and foo.f:
- Compile ibar.c using the icx compiler:
icx -qopenmp -c ibar.c
- Compile gbar.c using the GCC compiler:
gcc -fopenmp -c gbar.c
- Compile foo.f using the gfortran compiler:
gfortran -fopenmp -c foo.f
- 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:
Component directory layout example:
gfortran foo.o ibar.o gbar.o -lirc -liomp5 -lpthread -lc -L<install_dir>/lib
Unified directory layout example:
gfortran foo.o ibar.o gbar.o -lirc -liomp5 -lpthread -lc -L<install_dir>/<toolkit_version>/lib
where <install_dir> is the location of the installed Intel OpenMP library.
Alternately, you can use the Intel oneAPI DPC++/C++ 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:
- Compile the C source with the icx compiler:
icx -qopenmp -c ibar.c
- Compile the GNU Fortran source with gfortran:
gfortran -fopenmp -c foo.f
- Use icx to link the resulting object files with the -l option to pass the needed gfortran libraries:
icx -qopenmp foo.o ibar.o -lgfortran -L<install_dir_of_gfortran_libraries>
Windows Examples
This section shows several examples of using OpenMP with the Intel® C++ 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 oneAPI DPC++/C++ Compiler 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:
icx /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 oneAPI DPC++/C++ Compiler
You can use the Intel oneAPI DPC++/C++ 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:
- 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. - Compile f3.c and f4.c with the icx compiler, using the /Qopenmp option to enable OpenMP support:
icx /MD /Qopenmp /c f3.c f4.c
- Use the icx compiler to link the resulting application object code with the Intel C++ Compiler OpenMP library:
icx /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 oneAPI DPC++/C++ Compiler 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:
Open the project's property pages in from the main menu: Project > Properties (or right-click the Project name and select Properties) .
Select Configuration Properties > Linker > General > Additional Library Directories.
Enter the path to the Intel®-provided compiler libraries. For example:
<Intel_compiler_installation_path>\<version>\lib
Make the Intel OpenMP dynamic runtime library accessible at runtime; you must specify the corresponding path:
Open the project's property pages in from the main menu: Project > Properties (or right-click the Project name and select Properties).
Select Configuration Properties > Debugging > Environment.
Enter the path to the Intel®-provided compiler libraries. For example:
C:\Program Files (x86)\Common Files\intel\Shared Libraries
Add the Intel OpenMP runtime library name to the linker options and exclude the default Microsoft OpenMP runtime library:
Open the project's property pages in from the main menu: Project > Properties (or right-click the Project name and select Properties).
Select Configuration Properties > Linker > Command Line > Additional Options.
Enter the OpenMP library name and the Visual C++ linker option, /nodefaultlib:vcomp libiomp5md.lib.