# Program compiled with Gfortran does not write to output file on macOS Monterey

**URL:** <https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297>\
**Category:** Help\
**Created:** [November 18, 2021, 6:19pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297 "2021-11-18T18:19:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![quadir7719](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@quadir7719](https://fortran-lang.discourse.group/u/quadir7719)\
**Post date:** [November 18, 2021, 6:19pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/1 "2021-11-18T18:19:25Z")

</div>

Hello guys,  
I am running a .f90 code. I want output in some file. but after compiling and running, only the output file is created. There is no data in that but the same code is showing data in the terminal window. I tried with \> file.out too, but still the same issue.  
This is happening after updating Monterey. I reinstall GCC and gfortran as well.

---

<div class="post-metadata">

**Author:** ![wclodius](https://avatars.discourse-cdn.com/v4/letter/w/87869e/32.png) [@wclodius](https://fortran-lang.discourse.group/u/wclodius)\
**Post date:** [November 18, 2021, 6:55pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/2 "2021-11-18T18:55:34Z")

</div>

It appears that you are writing to either standard out or standard error. If so either your open statement is using the unit number for one of those, or your open statement does not have a filename, or your write statements are not using the unit number in your open statement. Please show some code showing the open and write statements.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [November 18, 2021, 6:59pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/3 "2021-11-18T18:59:57Z")

</div>

Welcome to the forum. Have you checked that a toy program such as

```auto
program main
implicit none
open (unit=20,file="temp.txt",action="write",status="replace") ! connect unit 20 to temp.txt
write (20,*) "abc" ! write to temp.txt
end program main

```

writes output to the specified file? Can you post your code?

---

<div class="post-metadata">

**Author:** ![mhulsen](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@mhulsen](https://fortran-lang.discourse.group/u/mhulsen)\
**Post date:** [November 18, 2021, 7:52pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/4 "2021-11-18T19:52:59Z")

</div>

Have the same problem here after upgrading to Monterey. A workaround for me is piping standard output through cat. So a.out | cat \>file.out instead of a.out \>file.out.

---

<div class="post-metadata">

**Author:** ![quadir7719](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@quadir7719](https://fortran-lang.discourse.group/u/quadir7719)\
**Post date:** [November 18, 2021, 7:55pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/5 "2021-11-18T19:55:25Z")

</div>

I run your code too but only empty get the output file.  
Here is my code whose printing on the terminal is fine but write command is not working

program main  
implicit real(a-h,o-z)  
open (unit=20,file=“temp.txt”,action=“write”,status=“replace”) ! connect unit 20 to temp.txt  
do i=1,10  
print\*, “hello”  
write (20,\*) “abc” ! write to temp.txt  
end do  
end program main

 ![Screenshot 2021-11-19 at 1.22.56 AM](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/a/a23850226da72e4d9124bfab14b8c6158386b24e.jpeg)

---

<div class="post-metadata">

**Author:** ![quadir7719](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@quadir7719](https://fortran-lang.discourse.group/u/quadir7719)\
**Post date:** [November 18, 2021, 7:58pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/6 "2021-11-18T19:58:49Z")

</div>

> [@mhulsen](#):
>
> a.out | cat \>file.out

is there any permanent solution so we can work as the previous way ??

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [November 18, 2021, 8:00pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/7 "2021-11-18T20:00:54Z")

</div>

I think you are hitting this issue: [102992 – fortran: redirecting standard out to a file does not work on macOS 12.0](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102992), the workaround for the time being is to set the environment variable `GFORTRAN_UNBUFFERED_ALL=1`.

* * *

We recently got a user report on one of our projects on this, here is the thread in case any of you is interested:

> <https://github.com/grimme-lab/crest/issues/73#issuecomment-967047193>
>
> Is it possible to build CREST on the macOS M1? Looks like some issues with MKL/O…penBLAS cannot be resolved automatically:
> 
> \`env LDFLAGS="-L/opt/homebrew/opt/openblas/lib" CC=gcc-11 FC=gfortran-11 meson setup \_build\_gcc --prefix=$PWD/\_dist -Dla\_backend=openblas
> 
> The Meson build system
> Version: 0.59.1
> Source dir: /Users/alex/src/crest-2.11.1
> Build dir: /Users/alex/src/crest-2.11.1/\_build\_gcc
> Build type: native build
> Project name: crest
> Project version: 2.11.0
> Fortran compiler for the host machine: gfortran-11 (gcc 11.1.0 "GNU Fortran (Homebrew GCC 11.2.0) 11.1.0")
> Fortran linker for the host machine: gfortran-11 ld64 650.9
> C compiler for the host machine: gcc-11 (gcc 11.1.0 "gcc-11 (Homebrew GCC 11.2.0) 11.1.0")
> C linker for the host machine: gcc-11 ld64 650.9
> Host machine cpu family: aarch64
> Host machine cpu: arm64
> Run-time dependency OpenMP found: YES 4.5
> Found pkg-config: /opt/homebrew/bin/pkg-config (0.29.2)
> Found CMake: /opt/homebrew/bin/cmake (3.21.2)
> Run-time dependency openblas found: NO (tried pkgconfig and framework)
> Library openblas found: YES
> config/meson.build:100: WARNING: Passed invalid keyword argument "exe\_deps".
> WARNING: This will become a hard error in the future.
> Run-time dependency lapack found: YES
> Program asciidoctor found: YES (/opt/homebrew/bin/asciidoctor)
> Configuring crest.1 with command
> Build targets in project: 1
> 
> Found ninja-1.10.2 at /Users/alex/miniforge3/envs/py39/bin/ninja\`
> 
> And following install command finishes with an error:
> 
> \`meson install -C \_build\_gc
> \<...\>
> gfortran-11 -o crest crest.p/src\_acidbase.f90.o crest.p/src\_atmasses.f90.o crest.p/src\_axis3.f.o crest.p/src\_biasmerge.f90.o crest.p/src\_bondconstraint.f90.o crest.p/src\_ccegen.f90.o crest.p/src\_classes.f90.o crest.p/src\_cleanup.f90.o crest.p/src\_compress.f90.o crest.p/src\_confcross.f90.o crest.p/src\_confopt.f90.o crest.p/src\_confparse.f90.o crest.p/src\_confscript1.f90.o crest.p/src\_confscript2\_main.f90.o crest.p/src\_confscript2\_misc.f90.o crest.p/src\_confscript3.f90.o crest.p/src\_constraining.f90.o crest.p/src\_cregen.f90.o crest.p/src\_data.f90.o crest.p/src\_deprotonate.f90.o crest.p/src\_dft\_propcalc.f90.o crest.p/src\_drsp.f.o crest.p/src\_ensemblecomp.f90.o crest.p/src\_entropic.f90.o crest.p/src\_entropy.f90.o crest.p/src\_eval\_timer.f90.o crest.p/src\_filemod.f90.o crest.p/src\_flexi.f90.o crest.p/src\_floydwarshall.f90.o crest.p/src\_freqmasses.f90.o crest.p/src\_geo.f90.o crest.p/src\_identifiers.f90.o crest.p/src\_intmodes.f.o crest.p/src\_io.f.o crest.p/src\_iomod.F90.o crest.p/src\_ligands.f90.o crest.p/src\_ls\_rmsd.f90.o crest.p/src\_marqfit.f.o crest.p/src\_minitools.f90.o crest.p/src\_msmod.f90.o crest.p/src\_msreact.f90.o crest.p/src\_ncigeo.f90.o crest.p/src\_neighbor.f.o crest.p/src\_newcregen.f90.o crest.p/src\_normmd.f90.o crest.p/src\_ompmklset.f90.o crest.p/src\_optinplace.f90.o crest.p/src\_pka.f90.o crest.p/src\_printouts.f90.o crest.p/src\_prmat.f.o crest.p/src\_progressbar.f90.o crest.p/src\_propcalc.f90.o crest.p/src\_protonate.f90.o crest.p/src\_quicksort.f90.o crest.p/src\_reactor.f90.o crest.p/src\_readl2.f.o crest.p/src\_readl.f.o crest.p/src\_relaxensemble.f90.o crest.p/src\_rotcompare.f90.o crest.p/src\_scratch.f90.o crest.p/src\_sdfio.f90.o crest.p/src\_select.f90.o crest.p/src\_setdg.f90.o crest.p/src\_shortpath.f90.o crest.p/src\_signal.c.o crest.p/src\_sigterm.f90.o crest.p/src\_solve.f.o crest.p/src\_solvtool.f90.o crest.p/src\_sortens.f90.o crest.p/src\_spline.f90.o crest.p/src\_stereoisomers.f90.o crest.p/src\_strucreader.f90.o crest.p/src\_symmetry2.f90.o crest.p/src\_symmetry\_i.c.o crest.p/src\_syscheck.f90.o crest.p/src\_tautomerize.f90.o crest.p/src\_thermocalc.f90.o crest.p/src\_thermo.f90.o crest.p/src\_trackorigin.f90.o crest.p/src\_trialmd.f90.o crest.p/src\_utilities.f.o crest.p/src\_xtbmodef.f90.o crest.p/src\_zdata.f90.o crest.p/src\_zfunctional.f90.o crest.p/src\_zsort.f90.o crest.p/src\_ztopology.f90.o crest.p/src\_crest\_main.f90.o -L/opt/homebrew/opt/openblas/lib -Wl,-dead\_strip\_dylibs -Wl,-headerpad\_max\_install\_names -Wl,-undefined,error -fopenmp -lopenblas -ldl -lm /Users/alex/miniforge3/envs/py39/lib/libopenblas.dylib -lgfortran
> Undefined symbols for architecture arm64:
> "\_mkl\_scoomm\_", referenced from:
> \_intens\_ in src\_solve.f.o
> "\_mkl\_set\_num\_threads\_", referenced from:
> \_ompautoset\_ in src\_ompmklset.f90.o
> ld: symbol(s) not found for architecture arm64
> collect2: error: ld returned 1 exit status
> ninja: build stopped: subcommand failed.
> Could not rebuild /Users/alex/src/crest-2.11.1/\_build\_gcc\`

---

<div class="post-metadata">

**Author:** ![quadir7719](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@quadir7719](https://fortran-lang.discourse.group/u/quadir7719)\
**Post date:** [November 18, 2021, 8:01pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/8 "2021-11-18T20:01:19Z")

</div>

> [@quadir7719](#):
>
> I run your code too but only empty get the output file.  
> Here is my code whose printing on the terminal is fine but write command is not working
> 
> program main  
> implicit real(a-h,o-z)  
> open (unit=20,file=“temp.txt”,action=“write”,status=“replace”) ! connect unit 20 to temp.txt  
> do i=1,10  
> print\*, “hello”  
> write (20,\*) “abc” ! write to temp.txt  
> end do  
> end program main

 ![Screenshot 2021-11-19 at 1.22.56 AM](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/a/a23850226da72e4d9124bfab14b8c6158386b24e.jpeg)

---

<div class="post-metadata">

**Author:** ![quadir7719](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@quadir7719](https://fortran-lang.discourse.group/u/quadir7719)\
**Post date:** [November 18, 2021, 8:08pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/9 "2021-11-18T20:08:42Z")

</div>

yes, I go with the thread. Same issue. In the meantime, Should we wait for the update or any solution?

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [November 18, 2021, 8:38pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/10 "2021-11-18T20:38:46Z")

</div>

Let’s keep the discussion here on discourse 😉

---

<div class="post-metadata">

**Author:** ![quadir7719](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@quadir7719](https://fortran-lang.discourse.group/u/quadir7719)\
**Post date:** [November 18, 2021, 8:47pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/11 "2021-11-18T20:47:07Z")

</div>

I tried with ./a.out | cat \> file.out  
It worked.

---

<div class="post-metadata">

**Author:** ![quadir7719](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@quadir7719](https://fortran-lang.discourse.group/u/quadir7719)\
**Post date:** [November 18, 2021, 8:49pm UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/12 "2021-11-18T20:49:18Z")

</div>

Is it fine to work with the previous version of macOS? I think it is fine with it.

---

<div class="post-metadata">

**Author:** ![JohnCampbell](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@JohnCampbell](https://fortran-lang.discourse.group/u/JohnCampbell)\
**Post date:** [November 19, 2021, 3:57am UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/13 "2021-11-19T03:57:58Z")

</div>

This post is very unclear to me !  
Forget about " | cat \>file.out", what happens when the executable runs ?  
What output is not being produced ?

Is temp.txt being produced ? \> if not, could imply gfortran is not providing data in output file.  
Is file.out being produced ? \> if not, could imply MacOS is not providing data in output file.

---

<div class="post-metadata">

**Author:** ![quadir7719](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@quadir7719](https://fortran-lang.discourse.group/u/quadir7719)\
**Post date:** [November 19, 2021, 6:08am UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/14 "2021-11-19T06:08:31Z")

</div>

> [@quadir7719](#):
>
> program main  
> implicit real(a-h,o-z)  
> open (unit=20,file=“temp.txt”,action=“write”,status=“replace”) ! connect unit 20 to temp.txt  
> do i=1,10  
> print\*, “hello”  
> write (20,\*) “abc” ! write to temp.txt  
> end do  
> end program main

See, if I am running the following code

program main  
implicit real(a-h,o-z)  
open (unit=20,file=“temp.txt”,action=“write”,status=“replace”) ! connect unit 20 to temp.txt  
do i=1,10  
write (20,\*) “abc” ! write to temp.txt  
end do  
end program main

The output “abc” should be written in the “temp.txt” file. Right?  
But this is not happening. This code creates the “temp.txt” file but this file remains empty.

If I run this code print instead of write, it shows output in the terminal as per expectation.

program main  
implicit real(a-h,o-z)  
open (unit=20,file=“temp.txt”,action=“write”,status=“replace”) ! connect unit 20 to temp.txt  
do i=1,10  
print\*, “abc” ! write to temp.txt  
end do  
end program main

---

<div class="post-metadata">

**Author:** ![quadir7719](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@quadir7719](https://fortran-lang.discourse.group/u/quadir7719)\
**Post date:** [November 19, 2021, 6:10am UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/15 "2021-11-19T06:10:35Z")

</div>

it means, the output file is producing but the output is not showing in that output file.

---

<div class="post-metadata">

**Author:** ![JohnCampbell](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@JohnCampbell](https://fortran-lang.discourse.group/u/JohnCampbell)\
**Post date:** [November 20, 2021, 4:27am UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/16 "2021-11-20T04:27:56Z")

</div>

Thanks for the update.  
Definately looks like a gfortran implementation problem.  
Would flush(unit=20) or close(unit=20) have any effect ?

---

<div class="post-metadata">

**Author:** ![wiremoons](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/wiremoons/32/764_2.png) [@wiremoons](https://fortran-lang.discourse.group/u/wiremoons)\
**Post date:** [November 20, 2021, 7:29am UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/17 "2021-11-20T07:29:16Z")

</div>

Using `flush(unit=20)` on its own does not work. However, when using `close(unit=20)` it does work ok, and the file contains the output expect. Without `close(unit=20)` a zero byte (empty file) is created only.

```auto
! Example program to write text to a file
! Build with: gfortran -static-libgcc -o write_file write_file.f90

program write_file
    implicit none
    open (unit=20,file="temp.txt",action="write",status="replace") ! connect unit 20 to temp.txt
    write (20,*) "abc" ! write to temp.txt
    close(unit=20)
end program write_file

```

---

<div class="post-metadata">

**Author:** ![quadir7719](https://avatars.discourse-cdn.com/v4/letter/q/7ab992/32.png) [@quadir7719](https://fortran-lang.discourse.group/u/quadir7719)\
**Post date:** [November 20, 2021, 8:18am UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/18 "2021-11-20T08:18:32Z")

</div>

no effect.  
The same code is working with Linux(ubuntu and fedora) and windows but not with macOS Monterey.

---

<div class="post-metadata">

**Author:** ![mhulsen](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@mhulsen](https://fortran-lang.discourse.group/u/mhulsen)\
**Post date:** [December 14, 2021, 9:57am UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/19 "2021-12-14T09:57:32Z")

</div>

Fixed in the latest update of gcc on Homebrew.

---

<div class="post-metadata">

**Author:** ![nicholaswogan](https://avatars.discourse-cdn.com/v4/letter/n/f1d935/32.png) [@nicholaswogan](https://fortran-lang.discourse.group/u/nicholaswogan)\
**Post date:** [January 7, 2022, 7:36am UTC](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297/20 "2022-01-07T07:36:09Z")

</div>

@mhulsen what version of gfortran works for you in Monterey? 11.2?

[Next page](https://fortran-lang.discourse.group/t/program-compiled-with-gfortran-does-not-write-to-output-file-on-macos-monterey/2297.md?page=2)
