My first Fortran program with CMake

This is my first time using CMake and I have just installed it.

cmake version 3.27.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Now, I am trying to run the following project:

GitHub - samanseifi/electroporation: Phase-Field Model of Electroporation

the steps there are:

mkdir build

cd build

cmake ..

i got this output

Capture

I have already been working with intel and gfortran compiler. But this shows the compiler error!

How to correct this problem?

You need Intel’s Visual Studio extension to support Fortran using that generator. You may want to use -GNinja instead which can use those compilers directly.

1 Like

I would suggest using the Intel oneAPI “x64 Native Tools Command Prompt” so that the environment is set optimally for oneAPI. It’s in the Windows start menu under Apps/Intel oneAPI typically.

In general instead of the three commands you typed above you can do

cmake -B build -G Ninja

If you don’t have Ninja, you can get from file ninja-win.zip at Releases · ninja-build/ninja · GitHub
Put the file Ninja.exe somewhere and add its location to Path environment variable.

I use oneAPI for Fortran on Windows with CMake a lot and it generally works well.

1 Like

Thanks for the detailed reply.

I have set path as:

ninja_path

The output looks like:

maybe the “git clone” didn’t copy the source files to that dircectory? Here what I did from scratch.

  1. Open “Intel oneAPI Command Prompt for Intel 64 for Visual Studio 2022” from the Windows Start menu
  2. git clone https://github.com/samanseifi/electroporation
  3. cd electroporation
  4. cmake -B build

which results in:

-- Building for: Ninja
-- The Fortran compiler identification is IntelLLVM 2023.2.0 with MSVC-like command-line
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Check for working Fortran compiler: C:/Program Files (x86)/Intel/oneAPI/compiler/latest/windows/bin/ifx.exe - skipped
-- Configuring done (2.2s)
-- Generating done (0.0s)
-- Build files have been written to: electroporation/build

then I type

cmake --build build

which would create file “bin\electroporation.exe”, except there is a mistake in the electroporation project. It works with Gfortran but not with Intel oneAPI, which is a stricter compiler. The oneAPI compilation error is:

electroporation\src\util_mod.f90(131): error #6404: This name does not have a type, and must have an explicit type.   [RAND]
1               v1=2.0*rand()-1.0
-----------------------^

Then I do

cd bin   
ecectroporoation.exe

which gives output

 Global data:
 _________________________________
 sigma =   0.0000
 gamma =   0.3120
 epsilon =   0.2500
 dt = 0.0010
 dx = 0.5000
 tmax = 100000
 file_skip =   1000
 Nx =    256
 Ny =    256
 Initil File = no
 Initial = RANDOMSEED
 Electric field = on
 _________________________________

 Electric data:
 _________________________________
 Interior Cond. =   0.0010
 Exterior Cond. =   0.0010
 Lipid Capcitance =   0.0100
 Lipid Conductance =   0.0000
 Electrod Length =   0.0010
 Applied Potential =  -1.0000
 _________________________________

I would put ninja.exe under your home directory, so that it can be safely added to Path like:

mkdir %USERPROFILE%\bin
move c:\ninja.exe %USERPROFILE%\bin

then in your Path add %USERPROFILE%\bin and remove the entry for c:\ninja.exe

It does not work for Intel if i put this command

error

try first

set FC=ifx

The folder do show the files

with gfortran i get the Ninja error

probably don’t need the “-G Ninja” when using Gfortran.

or you can add “-DCMAKE_MAKE_PROGRAM=c:/ninja.exe” or whereever ninja is.

I feel this CMake is so difficult to learn :pensive:. Maybe I stick to Fortran package manager :expressionless:.

FPM has the same issue, if the environment isn’t setup correctly.

@scivision Thanks for your help and explanation. It is just too much for me now. Maybe I will try it next year.

I made a pull request to the Electroporation project to fix the Fortran code so it works with Intel oneAPI. GitHub - scivision/electroporation: Phase-Field Model of Electroporation

However I think you’re maybe not using the correct command prompt. Be sure you’re using from Windows Start menu “Intel oneAPI Command Prompt for Intel 64 for Visual Studio 2022”. Otherwise the compiler environment is incorrect and the compiler isn’t found.

Then, we have to have the location to “ninja.exe”. Put in in your %USERPROFILE%\bin directory as a good practice. When you type

dir %USERPROFILE%\bin

you should see the “ninja.exe” file.

Then do

git clone https://github.com/scivision/electroporation
cd electroporation

cmake -B build -G Ninja -DCMAKE_MAKE_PROGRAM=%USERPROFILE%\bin\ninja.exe

cmake --build build

this makes build\eletroporation.exe|

These are similar issues you’d have in any programming language with any build system:

  1. must have the correct compiler environment (command prompt)
  2. must have the path to the build system (make or ninja)

This script might help. It’s designed to work with many simple CMake projects.
It works to find the oneAPI compiler, download and use Ninja if not available.

copy it into the electroporation directory and run from any Terminal or Command Prompt (it will load the oneAPI environment for you)

cmake -P oneapi_cmake.cmake
Access is denied.
    0 file(s) moved.

OK, I don’t know why ninja couldn’t be moved.

That script I linked above will download a local copy of ninja. That script builds the CMake project for you, first loading the compiler environment and downloading Ninja.

CMake Error at CMakeLists.txt:2 (project):
Running

‘USERPROFILE\bin\ninja.exe’ ‘–version’

failed with:

The system cannot find the file specified

CMake Error: CMAKE_Fortran_COMPILER not set, after EnableLanguage
– Configuring incomplete, errors occurred!