# Flag -DCMAKE\_Fortran\_FLAGS not changing the Makefile

**URL:** https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523
**Category:** Usage
**Created:** [June 8, 2021, 12:23pm UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523 "2021-06-08T12:23:09Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![afernandezody](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@afernandezody](https://discourse.cmake.org/u/afernandezody)
#### Post date: [June 8, 2021, 12:23pm UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/1 "2021-06-08T12:23:09Z")

</div>

Hello,  
I need to set a flag (“Mfree”) for my Fortran compiler. However, using the -DCMAKE\_Fortran\_FLAGS is not making any difference. I have compared both Makefiles (with and without) and they are the same (except for the parent directory). The full command to invoke the compilation is:

> sudo /usr/local/bin/cmake -DCMAKE\_INSTALL\_PREFIX=/opt/NCEP -DCMAKE\_C\_COMPILER=/opt/openmpi/bin/mpicc -DCMAKE\_Fortran\_COMPILER=/opt/openmpi/bin/mpif90 -DCMAKE\_Fortran\_FLAGS=“-Mfree” -DCMAKE\_PREFIX\_PATH=/opt/netcdf /opt/ERASE/NCEPLIBS

Thanks.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [June 8, 2021, 8:32pm UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/2 "2021-06-08T20:32:04Z")

</div>

I do this by setting environment variables `FFLAGS` as per [CMAKE\_\_FLAGS — CMake 3.20.3 Documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html)

It’s only effective on the first CMake configure “cmake -B build” of the project.  
To make it command local on Unix-like as usual:

```auto
FFLAGS="-Mfree" cmake ...

```

---

<div class="post-metadata">

### Author: ![afernandezody](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@afernandezody](https://discourse.cmake.org/u/afernandezody)
#### Post date: [June 9, 2021, 12:01am UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/3 "2021-06-09T00:01:35Z")

</div>

Hi **@scivision** ,  
Unfortunately including FFLAGS="-Mfree" doesn’t help either. I also tried `$sudo /usr/local/bin/cmake --build /opt/NCEPLIBS/build --target clean` to clean up as much as possible but same result. Unless the issue is caused by running cmake as a superuser, I’m out of ideas. Thanks.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [June 9, 2021, 12:42am UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/4 "2021-06-09T00:42:41Z")

</div>

The build directory probably needs to be wiped out e.g. `rm -r /opt/NCEPLIBS/build` otherwise CMake will have already initialized CMAKE\_Fortran\_FLAGS and ignores FFLAGS.

---

<div class="post-metadata">

### Author: ![afernandezody](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@afernandezody](https://discourse.cmake.org/u/afernandezody)
#### Post date: [June 9, 2021, 12:51pm UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/5 "2021-06-09T12:51:20Z")

</div>

I wiped out everything and tried to build from a different directory as a user (rather than as sudo) but the result is still the same, it doesn’t take the flag and fails with the _Label field of continuation line is not blank_ error message while trying to compile.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [June 9, 2021, 1:17pm UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/6 "2021-06-09T13:17:50Z")

</div>

OK the problem is `sudo`. I tried this with a minimal CMakeLists.txt:

```cmake
cmake_minimum_required(VERSION 3.14)
project(foo Fortran)

message(STATUS "Env. FFLAGS: $ENV{FFLAGS}")
message(STATUS "CMAKE_Fortran_FLAGS: ${CMAKE_Fortran_FLAGS}")

```

```auto
FFLAGS="-fimplicit-none" cmake -B build

```

works as expected–both variables are populated.

However,

```auto
FFLAGS="-fimplicit-none" sudo cmake -B build

```

leaves both variables blank. This is true at least for Bash and Zsh.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [June 9, 2021, 1:20pm UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/7 "2021-06-09T13:20:22Z")

</div>

I had simply given incorrect shell syntax. This works as expected:

```auto
sudo FFLAGS="-fimplicit-none" cmake -B build

```

---

<div class="post-metadata">

### Author: ![afernandezody](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@afernandezody](https://discourse.cmake.org/u/afernandezody)
#### Post date: [June 9, 2021, 2:11pm UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/8 "2021-06-09T14:11:20Z")

</div>

Hi @scivision,  
Trying to compile as a user rather than sudo didn’t make the trick for me. At the end, I had to fork (from the developer) and add the CMAKE\_Fortran\_FLAGS to the options included in CmakeLists.txt. It seems that it has finally taken the flag although it’s generating a long list of errors caused by other parts of the code. Thanks.

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [June 9, 2021, 3:29pm UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/9 "2021-06-09T15:29:38Z")

</div>

Can you check wether **exporting** the environment variable `FFLAGS` prior to your cmake call does solve the issue? I suppose it has to do with the usage of `ExternalProject_Add` in the `CMakeLists.txt` file. The setting for `CMAKE_Fortran_FLAGS` is not propagated to the invocation of the `ExternalProject_Add` configure/build command. Setting the `FFLAGS=-Mfree` variable prior to the invocation of cmake does influence only the cmake command’s shell but not the subsequently started ones for `ExternalProject_Add` commands. And try to build that as a restricted user without using sudo.

```
  export FFLAGS=-Mfree
  cmake -B build_dir -S source_dir <options>
```

---

<div class="post-metadata">

### Author: ![afernandezody](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/e19b73/32.png) [@afernandezody](https://discourse.cmake.org/u/afernandezody)
#### Post date: [June 9, 2021, 3:33pm UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/10 "2021-06-09T15:33:25Z")

</div>

That was my 1st attempt before everything else. cmake is only taking the flags included in CmakeList.txt so the solution was to modify this file.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [June 9, 2021, 6:12pm UTC](https://discourse.cmake.org/t/flag-dcmake-fortran-flags-not-changing-the-makefile/3523/11 "2021-06-09T18:12:44Z")

</div>

OK. If using ExternalProject, as Volker notes `sudo` may be making more complexities with the environment variables propagating through.

I would also strongly encourage in general using build systems including CMake **without** sudo. Perhaps only use sudo on the `cmake --install build` step if needed.
