Visual Studio 2019 Intel Fortran项目编译源文件管控方法咨询
Hi Mustafa, let's tackle this problem step by step—you're right that manually excluding files gets tedious as your project grows, and there are much cleaner ways to handle this in Visual Studio 2019 with Intel Fortran. Below are three practical solutions, ordered by how well they integrate with the VS environment:
1. Use Preprocessor Macros + Conditional Compilation (Best for VS Integration)
This method keeps all your subroutine files in the project but only compiles the one you want via preprocessor flags. Here's how to set it up:
Step 1: Add conditional wrappers to your subroutine files
Open eachcol_X.f90file and wrap its entire content in preprocessor directives. For example:! col_1.f90 #ifdef USE_COL_1 subroutine col() ! Your code for col version 1 here end subroutine col #endifDo the same for
col_2.f90withUSE_COL_2, and so on for other versions.Step 2: Define the active macro in project settings
- Right-click your project in Solution Explorer → Properties.
- Navigate to Intel Fortran > Preprocessor > Preprocessor Definitions.
- Add the macro for the version you want to use (e.g.,
USE_COL_1). Click OK.
Step 3: (Optional) Create dedicated project configurations
To switch versions in one click:- Go to Build > Configuration Manager.
- Create new configurations (e.g.,
Debug_Col1,Release_Col2) by copying existing ones. - For each new configuration, repeat Step 2 and set the corresponding macro (e.g.,
USE_COL_2for theRelease_Col2config). Now you can switch versions just by changing the active configuration in VS's toolbar.
2. Use a Master Include File (Simpler File Management)
If you prefer to keep only one "col" file in your project, you can use a master file that dynamically includes the correct version based on preprocessor macros:
Step 1: Create a master
col.f90file
This file will act as a switch for your subroutine versions:! col.f90 #ifdef USE_COL_1 include 'col_1.f90' #elif defined(USE_COL_2) include 'col_2.f90' ! Add more cases for other versions #else ! Optional: Throw a compile error if no version is selected #error "No COL subroutine version specified! Define USE_COL_X in preprocessor settings." #endifStep 2: Configure your project files
- Add only
col.f90to your project (remove or mark othercol_X.f90files as "Text" instead of "Fortran Source" to prevent direct compilation: right-click the file → Properties → General > Item Type → select Text). - Follow Step 2 from the first solution to define the
USE_COL_Xmacro in your project settings.
- Add only
3. Use a Custom Makefile (For Command-Line Style Control)
If you want to leverage the ifort command syntax you mentioned, you can set up a custom Makefile and integrate it with VS:
Step 1: Create a Makefile
Save this asMakefilein your project directory (adjust filenames to match your project):# Define compiler and flags (match your Intel Fortran settings) FC = ifort FCFLAGS = /O2 /warn:all # Target for col version 1 col1: $(FC) $(FCFLAGS) col_1.f90 foo1.f90 fooN.f90 -o program.exe # Target for col version 2 col2: $(FC) $(FCFLAGS) col_2.f90 foo1.f90 fooN.f90 -o program.exe # Default target (optional) all: col1Step 2: Configure VS to use NMake
- Right-click your project → Properties.
- Go to Intel Fortran > General > Build System → select NMake.
- Navigate to NMake > Build Command Line and set it to
nmake col1(ornmake col2for version 2). - Set Output to your executable name (e.g.,
program.exe) so VS recognizes the build output.
Now when you press F5, VS will run the
nmakecommand to compile only the files you specified. To switch versions, just update the Build Command Line to use the corresponding target.
内容的提问来源于stack exchange,提问作者Mustafa Iqbal




