Visual Fortran/Visual C++ Mixed-Language Programs

When you understand and reconcile the calling, naming and argument passing conventions between Fortran and C, you are ready to build an application.

If you are using Visual C/C++ you can edit, compile and debug your code within the Microsoft visual development environment. If you are using another C compiler, you can edit your code within the visual development environment by selecting File/New and choosing Visual C/C++ source in the File tab or, after activating the editor, by selecting the View menu Properties item and selecting from the drop-down list.

However, if you are not using Visual C/C++, you must compile your code outside the Microsoft visual 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 visual development environment.

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:

  cl /c cmain.c
  DF cmain.obj forsubs.f90

The Fortran (DF) compiler accepts an object file for the main program written in C and compiled by the C compiler. The DF 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 can do the linking, regardless of which language the main program is written in; however, if you use the DF compiler first, you must include DFOR.LIB with the C compiler, and you might experience some difficulty with the version of LIBC.LIB used by the C compiler. For these reasons, you may prefer to use the C compiler first or get your project settings for both Fortran and C to agree on the default C library to link against making sure that your application links against one and only one copy of the C library.

When using the visual development environment to build your application, Fortran uses default libraries depending on the information specified in the Fortran tab in the Project menu, Settings item (Project Settings dialog box). You can also specify linker settings with the Linker tab in the Project Settings dialog box.

In the Fortran tab, within the Libraries category, the following options determine the default libraries selected:

The combinations of these options use the following libraries:

Static or DLL Project?Use Multi-Theaded Libraries? Use C Debug Libraries? Fortran Link Library Used C Link Library Used
Static No No dfor.lib libc.lib
Static NoYes dfor.lib libcd.lib
Static Yes No dformt.lib libcmt.lib
Static Yes Yes dformt.lib libcmtd.lib
DLL No No dfordll.lib (dforrt.dll) msvcrt.lib (msvcrt.dll)
DLL NoYes dfordlld.lib (dforrtd.dll) msvcrtd.lib (msvcrtd.dll)
DLL YesNo dformd.lib (dformd.dll) msvcrt.lib (msvcrt.dll)
DLL Yes Yes dformdd.lib (dformdd.dll) msvcrtd.lib (msvcrtd.dll)

For example, if you select the Single-threaded item in the Use Run-Time Libraries list in the Libraries category of the Fortran tab, this specifies that static (/libs:static), single-threaded (/nothreads), and non-debug (/nodbglibs) libraries will be linked against, namely Fortran library dfor.lib and C/C++ library libc.lib.

If you select Debug Multi-threaded DLL in the Use Run-Time Libraries list in the Libraries category of the Fortran tab, this specifies that DLL (/libs:DLL), multi-threaded (/threads), and debug (/dbglibs) libraries will be linked against, namely Fortran import library dformdd.lib and its DLL library dformdd.dll and C/C++ import library msvcrtd.lib and its DLL library msvcrtd.dll.

The way Visual C++ chooses libraries is also based upon the Project menu Settings item, but within the C/C++ tab. In the Code Generation category, the "Use run-time library" item lists the following C libraries:

Menu Item Selected CL Option or Project Type Enabled Default Library Specified in Object File
Single-threaded /ML libc.lib
Multithreaded /MT libcmt.lib
Multithreaded DLL /MD msvcrt.lib (msvcrt.dll)
Debug Single-threaded /MLd libcd.lib
Debug Multithreaded /MTd libcmtd.lib
Debug Multithreaded DLL /MDd msvcrtd.lib (msvcrtd.dll)

If you are using Microsoft Visual C/C++, the Microsoft visual development environment can create 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 can add C source files to your Fortran project or Fortran source files to a C project, and they will be compiled and linked automatically.

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.

When printing from Visual C++ programs while calling Fortran subprograms that also print, the output may not appear in the order you expect. In Visual C++, the output buffer contents are not written immediately, but written when the buffer is full, the I/O stream is closed or the program terminates normally. The buffer is said to be "flushed" when this occurs.

To make sure interleaving Visual C++ and Fortran program units print in the order expected, you can explicitly flush the Visual C++ buffers after an output command with the flushall, fflush, fclose, setbuf, or setvbuf Visual C++ library calls.

Multithreaded applications should have full multithread support, so if you use DFORMT.LIB, be sure LIBCMT.LIB is specified as a default library.