How to debug a Windows* Fortran application from the command line

Introduction

For Fortran applications built within Microsoft Visual Studio*, you can debug the program by setting a breakpoint at the first executable line and then pressing F5 (or clicking the GO button).  But if you built the application using command line tools, this method is not available.  This article describes how to begin debugging an application built from the command line.

Note: This technique requires that a supported version of Microsoft Visual Studio is installed. It will not work if you are using Microsoft Visual C++ Express Edition* or the Windows Platform SDK to support the compiler environment.

Building the Application

Add the /debug option to the ifort command when compiling and linking.  For example:

ifort /debug /Od hello.f90

or

ifort /c /debug /Od main.f90
ifort /c /debug /Od sub.f90
ifort /debug /exe:main.exe main.obj sub.obj

This will create a .pdb (Program Database) file alongside the executable.  The .pdb file is required for debugging. The /Od option disables optimization to enhance debugging.

Start Debugging

In order to start the debugger, you must be in a command prompt environment which includes definitions for Microsoft Visual Studio.  This is the default for an Intel Fortran "Build Environment" session.

Type the command:

devenv /debugexe hello.exe

where hello.exe is replaced with the name of your executable. If the program requires command line arguments, they may follow the executable name on the command line.

Wait for Visual Studio to finish loading.

You must now set a breakpoint at the main program.  To do this, select Debug > New Breakpoint > Break at Function... or press CTRL-B.  You will see a dialog similar to the following:

new-breakpoint.gif

If you have built an IA-32 architecture application, type into the Function box the name:

_MAIN__

Note that there is one underscore before MAIN and two underscores after.

If you have built an Intel® 64 architecture application, type into the Function box the name:

MAIN__

In this case there is no leading underscore and two trailing underscores.

Click OK.

Press F5 or click the Start Debugging button.  The program will now begin executing but will break at the Fortran main program.  From there you may set additional breakpoints as desired.

If at the end of the session you close Visual Studio and allow it to save the new Visual Studio solution it created, you can debug again just by opening the solution (.sln file).  Your previously set breakpoints will be preserved.

Questions?

If you have comments or questions specifically relating to this article, use the Comments box below. For other questions, please visit our user forum.

Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.