Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28446 Discussions

Debug and compile fortran files with Visual Studio 2019

Amar_B
Beginner
17,472 Views

I have first to apologize  for my poor level in english, I will try to do my best to be understood. Few day ago, I installed the Intel Fortran Compiler (ifort) on Windows 10, by installing the intel oneAPI Base Toolkit and the oneAPI HPC toolkit. The aim was to compare the compilation between  gfortran from GNU on linux and ifort on a windows computer. 

So I have 7 fortran files(.f), 6 have to be compiled. On linux, by using VS code and gfortran compiler, I create and configure task.json and a Makefile files to debug and compile my code. 

Now, back on Windows, the only way I used Visual studio 2019 was to write a simple C++ script with no dependancies. So I just have do clic on debug to compile and run my cpp code. 

When I installed the toolkits from intel on windows 10, I wanted to be sure that the additionnal compilers where included in Visual studio 2019. In fact, it was the case, because I have now the possibility to create fortran projects, and I secceeded to debug and execute a simple fortran code on 1 file (.f). 

My lack of knowleadges on how to use Visual Code will now appears. In fact, I don't know how to set up tasks to debug and compile my fortran files. So, by using only the command lines all works, here are the command I used :

1) First I go to the directory where the fortran files are. 

Then I set up the intel compilers environment by :

2)  "C:\Program Files (x86)\Intel\oneAPI\setvars.bat"

After that, I compile my code using ifort : 

2) ifort hfaxial7.f vector.f gmatrix.f pn.f newlan.f laso.f  -o hfaxial7.exe

And finally, I run the executable :

3) hfaxial7.exe

Now, on Visual Sudio 2019, I don't know first if a have to create a fortran project, then I copy my fortran files into it. Also how do we create I simple task and a makefile to debug and run those files. 

I should specify that I'm studying theorical physics and it's a fortran program for nuclear phycis. I don't have the background in computing to find the solution by myself. 

I let the fortran files if needed, and if I have to give more precisions.

Thank you, in advance for your valuable assistance

Best regards, 

Amar

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
17,433 Views

When you are using Visual Studio, you must create a project, which then resides in a "solution". Normally, when you create a new project, the solution is created automatically. A solution can hold more than one project and is something that builds an end result (executable, for example).

  • Start Visual Studio 2019 and click "Create a new project".
  • On the next screen, change the Language dropdown from "All languages" to Fortran.
  • Change the project type dropdown from "All project types" to "Console". You can leave the Platform dropdown alone. 

It will now look like this:

Screenshot 2021-02-24 152115.png

Since you have existing files, select Empty Project and click Next. The next screen will ask you to name the project and say where you want the new project/solution folder created. Click Create. On the new screen that appears, you'll see something like this on the right, reflecting your new project.

Screenshot 2021-02-24 152722.png

Now click Project > Add Existing Item... and select all your source files. Alternatively, you can use Project > Add Existing Items from Folder, or you can just "drag and drop" files onto "Source Files".

To build the project, select Build > Build Solution.

There is no built-in facility to create a makefile, though Visual Studio provides a tool called nmake.

To debug, you must first set a breakpoint at an executable statement (such as the first executable statement). If you don't do this, the debugger will just run to completion when you start debugging.

For more reading, see Using Microsoft Visual Studio* (intel.com)

 

View solution in original post

18 Replies
Steve_Lionel
Honored Contributor III
17,434 Views

When you are using Visual Studio, you must create a project, which then resides in a "solution". Normally, when you create a new project, the solution is created automatically. A solution can hold more than one project and is something that builds an end result (executable, for example).

  • Start Visual Studio 2019 and click "Create a new project".
  • On the next screen, change the Language dropdown from "All languages" to Fortran.
  • Change the project type dropdown from "All project types" to "Console". You can leave the Platform dropdown alone. 

It will now look like this:

Screenshot 2021-02-24 152115.png

Since you have existing files, select Empty Project and click Next. The next screen will ask you to name the project and say where you want the new project/solution folder created. Click Create. On the new screen that appears, you'll see something like this on the right, reflecting your new project.

Screenshot 2021-02-24 152722.png

Now click Project > Add Existing Item... and select all your source files. Alternatively, you can use Project > Add Existing Items from Folder, or you can just "drag and drop" files onto "Source Files".

To build the project, select Build > Build Solution.

There is no built-in facility to create a makefile, though Visual Studio provides a tool called nmake.

To debug, you must first set a breakpoint at an executable statement (such as the first executable statement). If you don't do this, the debugger will just run to completion when you start debugging.

For more reading, see Using Microsoft Visual Studio* (intel.com)

 
Amar_B
Beginner
17,389 Views

Thank you for this answer, I secceed in compiling and executing the program by using VS 2019 !

I notice that when I debug the code, I find many errors linked to mismatch variables but the executable is still released. Thoses errors are not mentionned when I compile using comand lines only. 

0 Kudos
mecej4
Honored Contributor III
17,377 Views

You are drawing incorrect conclusions regarding what follows when you invoke the compiler using the command line versus what it does when called from Visual Studio.

The compiler responds to the options that it is given. If you use the same options from the command line as those passed to the compiler by VS, the responses will also be the same. 

0 Kudos
Rick_S
Beginner
15,622 Views

I'm trying to compile a F95 program with several subroutines using VS 2019 with Intel Fortran 2022.

I added my subroutines to the Source file tab, and have the program file in the main VS screen. When I build the solution there are no errors found but an exe file is apparently not produced since starting the debug process I get a message that says '*.exe file not found'.

What could I be missing here?

0 Kudos
Ron_Green
Moderator
17,417 Views

I am curious what it is you are trying to compare - runtimes? Numerical answers? In both cases I expect differences don't you? You know you will probably get different answers, yes? You are not expecting the same answer out to 12 digits are you?

I just want to make sure you're not wasting your time. 2 different OSes, 2 different compilers == different numerical results. Not to mention runtimes. Since you are a Physicist, it's like saying " I want to throw a baseball on earth and on Mars and to compare the two." If you expect differences, great. If you expect roughly the same behavior then you're starting with a false assumption.


0 Kudos
Steve_Lionel
Honored Contributor III
17,408 Views

Ron, I think you meant that to be a reply to a different thread.

0 Kudos
Amar_B
Beginner
17,368 Views

Hi Ron, 

I understand your question, I went too fast to explain what I'm trying to compare and why. Actually, I use 2 different OS just because gcc compilers and so gfortran, is simpler to be installed and used on Linux. And, by reading some forums, I noticed that intel toolkits where also incorporated very well to VS 2019 IDE. So ifort was used on windows.

I know that for each OS, compilers have different behaviors.

The issue was that I taked this code wrote by my supervisor that makes big calculation in nuclear physics (the program find quadrupole momentum and binding energy of nucleus in isotopic chains). I should specify that the files were always compiled by my supervisor by using ifort from intel and on linux (ubuntu).

When I tested thoses files, I used gfortran (on linux) because I never heard about ifort or intel compilers before now. So I was surprised to see that the code was not compiled will gfortran, it signals to many errors and warnings. As you said it, I think it is due to the fact that the 2 compilers have 2 differents behaviors ; my assumption was not Ok, I thought that for a language (here fortran 95) 2 different compiler will just be different in the numerical answers or runtimes But if the compilation and the execution is a success with one compiler than it will be the same on the other ( as I said in my previous message, I didn't have lectures on theorical computing..but if you know some books to understand how it really functions, I would be interested ).

So finally, the compilation on ifort on windows is a success even if there are some errors when I debug. 

By the way, I have 2 questions on VS 2019 :

- Since I have installed HPC and bases toolkits from intel, how can I know what compiler VS uses when I run a C++ program ? (because I assume that there will be now 2 different C++ compilers on it, even if I don't know what was the default compiler when I installed VS without intel toolkits)

- Also, I have a question about debugggers. I think that the GDB debugger from GNU project is used  on VS by default. But when I installed the 2 intel toolkits, is VS IDE using IDB debugger know ? Also when I compile on the command prompt, what is the command to use GDB or an other debugger for example here : ifort hfaxial7.f vector.f gmatrix.f pn.f newlan.f laso.f  -o hfaxial7.exe (before the command line, I set up the intel environment) 

Thank you in advance for your answer,

Amar, 

0 Kudos
mecej4
Honored Contributor III
17,358 Views

Amar, many of your questions reveal that you are not yet capable of debugging this program. You will need to take courses, read books, talk to colleagues and learn how to debug Fortran programs of at least medium size.

Unfortunately, the present program is in need of debugging, since it has bugs. Here are two such bugs:

  • Line 654 of hfaxial.f has  Z = Z-114D-2*N**426-3/Z. When this statement is executed, N has the value 20. Raising 20 to the power 426 causes integer overflow, and it takes special effort to catch this event, and we cannot tell you how to correct this mistake. Ask your advisor.
  • If the overflow that I mentioned above is ignored, the program runs some more and then attempts to do unformatted I/O to the console unit, which is open for formatted transfer. We cannot tell you how to remedy such an error, either, since we cannot read the mind of the author of the program.

There may be many more bugs. Debugging a complex program is not a task for beginners.

0 Kudos
FortranFan
Honored Contributor II
17,302 Views

@Amar_B ,

You wrote in your original post, ".. how do we create I simple task and a makefile to debug and run those files. .." and other sentences that suggest you feel somewhat like a beginner and you would like to learn more about how to do code development and also debugging. 

If I understood your points correctly, I suggest you look at resources online such as TutorialsPoint, here is an example link for debugging is https://www.tutorialspoint.com/gnu_debugger/index.htm.  There are several other resources such as this one, an internet search engine is your friend. 

You may also want consider Visual Studio Code on Linux: https://code.visualstudio.com/docs/cpp/config-linux.  

And Visual Studio on Windows: https://docs.microsoft.com/en-us/visualstudio/debugger/getting-started-with-the-debugger-cpp?view=vs-2019

There are blogposts, YouTube videos, etc. also as you will know, or will soon find out if you try such searches.  You can then pick the learning format(s) that aligns with your way of learning.

On how to work with Fortran, please take a look at this tutorial: http://www.egr.unlv.edu/~ed/fortran.html

A book on Fortran I suggest is this: https://www.amazon.com/Fortran-Scientists-Engineers-Stephen-Chapman-ebook/dp/B06XCTY8KR

With above links I have provided, please note I am basing them on the following: the quantity and quality of information on how to develop and DEBUG C++ code is tremendous.  If you go over even a fraction of such material and gain a bit of ease with C++ debugging, you will be able to apply CERTAIN of those concepts to Fortran as well and it will help you in your work with Fortran also.   

 

0 Kudos
Steve_Lionel
Honored Contributor III
17,293 Views

Visual Studio (on Windows) does not use gdb for debugging - it uses its own, excellent debugger. For Intel Fortran, the Microsoft debugger is enhanced with Fortran-specific support, but it is still the Microsoft debugger. IDB, which was at one time the Intel Debugger for Linux, no longer exists.

To build an application for debugging from the command line, add the /debug option to all commands. While it is possible to invoke the debugger from the command line, it is not a command line debugger (it's the Visual Studio debugger) and it is far easier to do this from within Visual Studio with the project you built there.

The compiler defaults for a new VS project are different from those on the command line. Especially for a Debug configuration, additional compile-time and run-time checks are enabled. You encountered one of these, generated interface checking. From the command line this is /warn:interface . Another option enabled in a Debug configuration is array and string bounds checking (/check:bounds).

As for Intel C++, the default is that you are using Microsoft Visual C++. You can right click on a C/C++ project and you will then see an "Intel Compiler" option:

Screenshot 2021-02-25 111530.png

If you select the Intel C++ Compiler Classic (I suggest not using the DPC compiler unless you are using its features), the project will be reconfigured, and it will show you the selected compiler after the project name:

Screenshot 2021-02-25 111333.png

If you want to revert to Visual C++, return to the Intel Compiler option in the project menu.

Screenshot 2021-02-25 111441.png

 
0 Kudos
JohnNichols
Valued Contributor III
17,275 Views

@Amar_B 

I loaded all of the Fortran code into a VS 2019 solution and compiled it.  

The errors on a straight compile were 

Capture.PNG

At the moment it is not going to run.  

My suggestions: 

1. if this is a class assignment for a masters or PhD class - find out the compiler from the professor where this last successfully compiled and hope you can find it.   Ask for an exe files politely. 

2. If this is your masters topic work then you need to fix all of the errors -- there are 20000 lines of code so someone with say 40 years of experience it will take a long weekend of nothing else -- you might just get it running - this includes getting rid of all of the commons, implicit variables and finding all of the missed named variables.  The code was written in 1982 - there is a lot of potential errors that a modern compiler will pick up that the old compilers allowed.  If you are doing it then set aside a month of time to learn and do it.  At the end you will be a good programmer and intimately familiar with these people here. The six errors shown are just the tip of the iceberg, there are a lot there.  Reading the code made me smile. 

3. A modern compiler finds errors and ensures your code is more correct.  As you update the more you find. 

4. If you have to fix it - then you can start by asking on this forum what to fix first and we can go through the steps after you have read a book on Fortran. it is not difficult if you follow a path that is clear.  

5. If this is your PhD work - develop new code from scratch using the Intel MKL libraries and some really fast solvers. 

6. Understand this program was written by several people over 4 decades, you can see the problems.  Try reading Beowulf in the original and you can understand the problems with the age of the code. 

I would be honest with these people, what time do you have and how good does it have to be,  and then you can work out a strategy that works. 

 

Good luck. 

 

 

 

Steve_Lionel
Honored Contributor III
15,607 Views

What is the file type of the file? If it is .f95, change it to .f90. See also Doctor Fortran in "Source Form Just Wants to be Free" - Doctor Fortran (stevelionel.com)

If that is not the case, open Visual Studio, select Tools > Options > Intel Compilers and Libraries > IFORT Intel Fortran Classic > General. Under Free Format Extensions, make sure that it includes .f90. If not, add it and click OK.

(There used to be an Intel Knowledge Base article about this, but it got purged inappropriately.)

0 Kudos
JohnNichols
Valued Contributor III
15,596 Views

@Steve_Lionel  noted that "but it got purged inappropriately."

 

Stalin's methods perhaps. 

0 Kudos
Rick_S
Beginner
15,594 Views

Thanks for the reply. Under options the .f90 extension is included. The files are .f95 so I changed all files to .f90 with a text editor. The subroutines are included under the source tab in the solution editor. The compile process starts and produces the following error when referencing some of the subroutines.

Severity Code Description Project File Line Suppression State
Error error #7978: Required interface for passing assumed shape array is missing from original source. [Y_COORDS] D:\my docs\engineering\DVG\work6\p61.f90 53

Do I need to add an interface routine to the main code?

0 Kudos
Steve_Lionel
Honored Contributor III
15,584 Views

OK. So, the unrecognized file type meant that nothing got compiled (and therefore no executable.)

The new error is saying that your program is incorrect - you are calling a routine (Y_COORDS) which has an assumed-shape dummy argument, but no explicit interface is visible. Please read Doctor Fortran Gets Explicit - Again! - Doctor Fortran (stevelionel.com) for more details.

0 Kudos
Rick_S
Beginner
15,570 Views

I think I'm beyond the interface issue. I created a LIB project and compiled all subroutines into a static library (Lib1.lib) where VS created the _genmod.f90 and _genmod.mod files you mentioned in the link. These are the interface files I needed.

I have a VS solution with the main program project (p61.f90). I added the Lib1 project to the solution and added the Lib1.lib to the  dependencies of p61.f90.  At this point I'm not sure what files to add to the source of each project. I added the _genmod.mod files to the p61f90 source tab, and Lib1.lib to the Lib1 source. It compiles with no errors but does not create a exe file.  Do I have this set up correctly in VS? I'm not using a command line to compile but instead using the build solution option in VS.

Thanks-

0 Kudos
jimdempseyatthecove
Honored Contributor III
15,567 Views

>>where VS created the _genmod.f90 and _genmod.mod files

VS will(may) also create ..._genmod.obj files which will contain the data and contained procedures if/when present in the module(s). These too must be included in the Lib1.Lib. The VS project file for the library will generally do this. Note, while your library may get built with Build All on your solution, the .exe may fail to get built if its project is not dependent on the library project. IOW it will not be linked with the library. Right-Click on the .exe project in the solution explorer then  Build Dependencies | Project Dependencies then check the checkbox for your library project.

 

Also, it wouldn't hurt to check the Configuration Manager to assure the project(s) get built. Click the Pull-Down on the configuration (x64/Win32/x86) then click on Configuration Manager then verify the desired (usually all) project configurations are checked for build.

 

Jim Dempsey

0 Kudos
Steve_Lionel
Honored Contributor III
15,561 Views

No, the generated interface files are NOT what you need - or at least they are not intended as a substitute for an explicit interface in your code. It's fine to use the generated .genmod_f90 as a basis for writing your own interface block.

0 Kudos
Reply