You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Visual Studio 2019 Intel Fortran项目编译源文件管控方法咨询

Better Ways to Switch Fortran Subroutine Versions in 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 each col_X.f90 file 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
    #endif
    

    Do the same for col_2.f90 with USE_COL_2, and so on for other versions.

  • Step 2: Define the active macro in project settings

    1. Right-click your project in Solution Explorer → Properties.
    2. Navigate to Intel Fortran > Preprocessor > Preprocessor Definitions.
    3. 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:

    1. Go to Build > Configuration Manager.
    2. Create new configurations (e.g., Debug_Col1, Release_Col2) by copying existing ones.
    3. For each new configuration, repeat Step 2 and set the corresponding macro (e.g., USE_COL_2 for the Release_Col2 config). 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.f90 file
    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."
    #endif
    
  • Step 2: Configure your project files

    1. Add only col.f90 to your project (remove or mark other col_X.f90 files as "Text" instead of "Fortran Source" to prevent direct compilation: right-click the file → PropertiesGeneral > Item Type → select Text).
    2. Follow Step 2 from the first solution to define the USE_COL_X macro in your project settings.

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 as Makefile in 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: col1
    
  • Step 2: Configure VS to use NMake

    1. Right-click your project → Properties.
    2. Go to Intel Fortran > General > Build System → select NMake.
    3. Navigate to NMake > Build Command Line and set it to nmake col1 (or nmake col2 for version 2).
    4. 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 nmake command 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

火山引擎 最新活动