Visual Studio Code: cannot compile FORTRAN-Code

I added the “Modern Fortran” extension to Visual Studio Code (version: 1.84.2) and added the corresponding path to “fortls” (OS=WINDOWS 11 64 Bit).
When I now want to compile the FORTRAN-Code under “Run and debug”, I get the message: “No CMakeLists.txt was found”.
What code must the file “CMakeLists.txt” contain so that the FORTRAN-Code can be compiled?
Kind Regards

1 Like

Welcome to the Fortran discourse!
In addition to VS code and the modern Fortran plug in (with fortls) you will also need a compiler, gfortran and ifort(Intel OneAPI) are popular choices with LFortran gaining ground very quickly. Then you need a build system, and make with cmake are common choices there, but neither is required for small projects.
Bon voyage!

1 Like

thank you for youranswer.
I think I have it all:
Output of “Modern Fortran”

[INFO - 10:13:55 AM] Extension Name: Modern Fortran
[INFO - 10:13:55 AM] Extension Version: 3.4.0
[INFO - 10:13:55 AM] Linter set to: "gfortran"
[INFO - 10:13:55 AM] Formatter set to: "fprettify"
[INFO - 10:13:55 AM] Autocomplete set to: "fortls"
[INFO - 10:13:55 AM] Hover set to: "fortls"
[INFO - 10:13:55 AM] Symbols set to: "fortls"
[INFO - 10:13:55 AM] [lint] Found GNU Fortran version 13.2.0

I think I have a problem with cmake, see the following output:

[proc] Executing command: cmake --version
[proc] Executing command: cmake -E capabilities
[variant] Loaded new set of variants
[kit] Successfully loaded 12 kits from C:\Users\jbabe\AppData\Local\CMakeTools\cmake-tools-kits.json
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --version
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" -E capabilities
[proc] Executing command: c:\strawberry\Fortran\bin\gcc.exe -v
[proc] The command: ninja --version failed with error: Error: spawn ninja ENOENT
[proc] The command: ninja-build --version failed with error: Error: spawn ninja-build ENOENT
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" -SE:/MeineProgramme/Visual_Studio_Code/Fortran/Hello -Be:/MeineProgramme/Visual_Studio_Code/Fortran/Hello/build -G "Unix Makefiles"
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" -SE:/MeineProgramme/Visual_Studio_Code/Fortran/Hello -Be:/MeineProgramme/Visual_Studio_Code/Fortran/Hello/build -G "Unix Makefiles" exited with code: 1
[main] Configuring project: Hello 
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=c:\strawberry\Fortran\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=c:\strawberry\Fortran\bin\g++.exe -SE:/MeineProgramme/Visual_Studio_Code/Fortran/Hello -Be:/MeineProgramme/Visual_Studio_Code/Fortran/Hello/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] CMake Error at CMakeLists.txt:1:
[cmake]   Parse error.  Expected a command name, got unquoted argument with text
[cmake]   ".SUFFIXES:".
[cmake] -- Configuring incomplete, errors occurred!
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=c:\strawberry\Fortran\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=c:\strawberry\Fortran\bin\g++.exe -SE:/MeineProgramme/Visual_Studio_Code/Fortran/Hello -Be:/MeineProgramme/Visual_Studio_Code/Fortran/Hello/build -G "Unix Makefiles" exited with code: 1

Here is my CMakeList.txt



main: $(subst .f90,.o,$(SOURCES))
    $(FC) -o $@ $+

.PHONY: clean
    -rm -f *.o *.mod *.smod main

Kind Regards

1 Like

@JBaben , I think there are two majour issues in your last report.
Said very shortly (and very badly technically), cmake is a cross-platform tool that creates the build scripts for you, based on the OS you are running it on. Which in this case is Windows I understand.

So, the first issue I see is in

when launching the cmake command. the -G flag specifies the generator of such scripts. You are telling him to generate them for "Unix makefiles", which is as the name suggests, for UNIX OSes. Not windows, natively.

Then, the second (majour) problem, is that you wrote the CMakeLists.txt cmake script file not using the CMake scripting language, but that is Makefile scripting language instead.
So, CMake would never be capable of interpreting it.
If you know Makefile syntax, I’d suggest then to got for that and find the tool to build with Makefiles on Windows.

It was clear to me that I had a problem with “CMakeLists.txt”.
Even if I use the correct syntax for CMake:

cmake_minimum_required(VERSION 3.1)

project(hello VERSION 0.1
        DESCRIPTION "My Fortran program"
        LANGUAGES Fortran)

CMAKE_Fortran_FLAGS = gfortran

# Currently setting the Fortran compiler to use -std=gnu, change this if you
# want a specific standard
set(FVERSION "-std=f90")
set(CMAKE-FC-COMPILER /C:/Strawberry/Fortran/bin/pgfortran)
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${FVERSION}")

# Source code

install(TARGETS hello     DESTINATION "bin")

I always get this:
“C:\Program Files\CMake\bin\cmake.EXE” -SE:/MyPrograms/Visual_Studio_Code/Fortran/Hello -Be:/MyPrograms/Visual_Studio_Code/Fortran/Hello/build -G Ninja
Unfortunately, I don’t know where this “-G” comes from or is defined.
Kind Regards

1 Like

A cmake configuration file for a basic project would probably look closer to this

cmake_minimum_required(VERSION 3.10)

project(HelloWorldFortran VERSION 1.0 DESCRIPTION "Fortran Hello World Project" LANGUAGES Fortran)

add_executable(hello_world hello_world.f90)
  • configuration is done via
cmake -B build
  • building (or more often referred to as compiling ) is done via
cmake --build build

Fortran, C and C++ projects work pretty much the same under cmake. You might want to have a look at the tutorials on the cmake documentation website, for the syntax and an easy intro.

-G stands for a generator see

thank you very much for the “CMakeLists.txt”, unfortunately it doesn’t work yet (it seems that some assignments are still missing: CMAKE_WWORLD_COMPILER_ENV_VAR)
With “cmake -B build” I get the following messages:

CMake Warning at CMakeLists.txt:3 (project):
  the following parameters must be specified after LANGUAGES keyword: WWorld,

CMake Error: Could not find cmake module file: CMakeDetermineWWorldCompiler.cmake
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMake Error: Could not find cmake module file: E:/MeineProgramme/Visual_Studio_Code/Fortran/Hello_World/build/CMakeFiles/3.19.4/CMakeWWorldCompiler.cmake
-- The Fortran compiler identification is GNU 13.2.0
CMake Error at CMakeLists.txt:3 (project):
  No CMAKE_WWorld_COMPILER could be found.

  Tell CMake where to find the compiler by setting the CMake cache entry
  CMAKE_WWorld_COMPILER to the full path to the compiler, or to the compiler
  name if it is in the PATH.

CMake Error: Could not find cmake module file: CMakeWWorldInformation.cmake
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - failed
-- Check for working Fortran compiler: C:/Strawberry/Fortran/bin/gfortran.exe
-- Check for working Fortran compiler: C:/Strawberry/Fortran/bin/gfortran.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.19/Modules/CMakeTestFortranCompiler.cmake:51 (message):
  The Fortran compiler

  is not able to compile a simple test program.
  It fails with the following output:
    Change Dir: E:/MeineProgramme/Visual_Studio_Code/Fortran/Hello_World/build/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/Strawberry/c/bin/gmake.exe cmTC_b15d0/fast && C:/Strawberry/c/bin/gmake.exe  -f CMakeFiles/cmTC_b15d0.dir/build.make CMakeFiles/cmTC_b15d0.dir/build
    gmake.exe[1]: Entering directory 'E:/MeineProgramme/Visual_Studio_Code/Fortran/Hello_World/build/CMakeFiles/CMakeTmp'
    gmake.exe[1]: *** [CMakeFiles/cmTC_b15d0.dir/build.make:84: CMakeFiles/cmTC_b15d0.dir/testFortranCompiler.f.obj] Error -1073741502
    gmake.exe[1]: Leaving directory 'E:/MeineProgramme/Visual_Studio_Code/Fortran/Hello_World/build/CMakeFiles/CMakeTmp'

    gmake.exe: *** [Makefile:140: cmTC_b15d0/fast] Error 2

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)

CMake Error: CMAKE_WWorld_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "E:/MeineProgramme/Visual_Studio_Code/Fortran/Hello_World/build/CMakeFiles/CMakeOutput.log".
See also "E:/MeineProgramme/Visual_Studio_Code/Fortran/Hello_World/build/CMakeFiles/CMakeError.log".

Kind Regards

my solution: I now use: Visual Studio 2022 with the Intel Fortran compiler ifort/ifx.
This allows me to create Fortran source files and create the program.
Kind Regards

1 Like