Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 7/13/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Build Intel® Fortran and C Mixed-Language Programs on Windows

Understand how to call, name, and argument passing conventions between Fortran and C to build an application. If you are using Microsoft Visual C++ or Intel® C++, you can edit, compile, and debug your code within the Microsoft Integrated Development Environment. If you are using another C compiler, you can edit your code within the Integrated Development Environment. However, you must compile your code outside the Integrated Development Environment and either build the Fortran/C program on the command line or add the compiled C .OBJ file to your Fortran project in the Microsoft IDE.

As an example of building from the command line, if you have a main C program CMAIN.C that calls Fortran subroutines contained in FORSUBS.F90, you can create the CMAIN application with the following commands:

icl /c cmain.c
ifort cmain.obj forsubs.f90

Intel® Visual Fortran accepts an object file for the main program written in C and compiled by a C compiler. The compiler compiles the .F90 file and then has the linker create an executable file under the name CMAIN.EXE using the two object files.

Either compiler (C or Intel® Visual Fortran) can do the linking, regardless of which language the main program is written in. However, if you use the Intel® Visual Fortran compiler first, you must include LIBIFCORE.LIB and IFCONSOL.LIB with the C compiler. You may experience some difficulty with the version of the runtime library used by the C compiler. You may prefer to use the C compiler first or specify your project settings for both Intel® Visual Fortran and C so there is agreement on the C library to link against. Ensure that your application links against only one copy of the C library.

If you are using the IDE to build your application, Intel® Visual Fortran uses Fortran and C libraries depending on the information specified in the Fortran folder in Project > Properties (Project Settings dialog box). You can also specify linker settings with the Linker folder in the Project Settings dialog box.

In the Fortran folder, within the Libraries property page, the Runtime Library category determines the libraries selected.

Runtime Library

Fortran Link Library Used

C Link Library Used

Debug Single-Threaded

libifcore.lib

libcmtd.lib

Multithreaded

libifcoremt.lib

libcmt.lib

Debug Multithreaded

libifcoremt.lib

libcmtd.lib

Debug Single-Threaded DLL

libifcorertd.lib (libifcorertd.dll)

msvcrt.lib (msvcrt.dll)

Multithreaded DLL

libifcoremd.lib (libifcoremd.dll)

msvcrt.lib (msvcrt.dll)

Debug Multithreaded DLL

libifcoremdd.lib (libifcoremdd.dll)

msvcrt.lib (msvcrt.dll)

For example, select Debug Multi-threaded DLL in the runtime Library list to specify that DLL (/libs:DLL), multi-threaded (/threads), and debug (/dbglibs) libraries should be linked against, namely Fortran import library libifcoremdd.lib and its DLL library libifcoremdd.dll and C/C++ import library msvcrtd.lib and its DLL library msvcrtd.dll.

A mixed language solution containing a Fortran library project should have Disable Default Library Search Rules set to No in the IDE. To check this setting, choose Project > Properties and then choose the Libraries category. If you change the Disable Default Library Search Rules setting to Yes, you will need to explicitly add the needed runtime libraries to the non-Fortran project. If you are adding libraries explicitly, make sure you add IFCONSOL.LIB to the libraries of the non-Fortran project. This library is needed to perform almost any kind of I/O with Intel® Visual Fortran.

When you have a C++ main program and a Fortran library subproject, you need to manually add the library path to the Intel® Visual Fortran LIB folder. You only need to do this manual add once per user. To add the path, choose Tools > Options > Projects and Solutions > VC++ Directories. Use the Show directories for: dropdown item to select Library files. Add the path to the displayed list.

The way Microsoft Visual C++ chooses libraries is also based upon the Project > Properties item, but within the C/C++ tab. In the Code Generation category, the runtime library item lists the following C libraries:

Menu Item Selected

CL Option or Project Type Enabled

Default Library Specified in Object File

Multithreaded

/MTd

libcmt.lib

Multithreaded DLL

/MD

msvcrt.lib (msvcrnn.dll)

Debug Multithreaded

/MTd

libcmtd.lib

Debug Multithreaded DLL

/MDd

msvcrtd.lib (msvcrtd.dll)

You must take care to choose the same type of runtime libraries in both your Fortran and C project. For example, if you select Multithreaded DLL in your Fortran project, you must select Multithreaded DLL in your C project. Otherwise, when your build your mixed Fortran/C application, you will receive errors from the Linker regarding undefined and/or duplicate symbols.

If you are using Microsoft Visual C++ or Intel® C++, the Microsoft Integrated Development Environment can build mixed Fortran/C applications transparently, with no special directives or steps on your part. You can edit and browse your C and Fortran programs with appropriate syntax coloring for the language. You need to place your Fortran source files into a Fortran project and your C/C++ files into a Visual C++ project.

When you debug a mixed Visual C++/Fortran application, the debugger will adjust to the code type as it steps through: the C or Fortran expression evaluator will be selected automatically based on the code being debugged, and the stack window will show Fortran data types for Fortran procedures and C data types for C procedures.