# Actually Portable Executable

**URL:** <https://fortran-lang.discourse.group/t/actually-portable-executable/4764>\
**Category:** Uncategorized\
**Created:** [November 18, 2022, 9:19pm UTC](https://fortran-lang.discourse.group/t/actually-portable-executable/4764 "2022-11-18T21:19:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![feenberg](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@feenberg](https://fortran-lang.discourse.group/u/feenberg)\
**Post date:** [November 18, 2022, 9:19pm UTC](https://fortran-lang.discourse.group/t/actually-portable-executable/4764/1 "2022-11-18T21:19:19Z")

</div>

At [https://justine.lol/ape.html](https://justine.lol/ape.html) Justine Tunney describes a way to make a single binary executable that will run in Windows, Linux, or OSX/BSD. I followed the instructions and was able to make hello.c work this way, but at the link step a Fortran source hello.f just generated a bunch of undefined reference error messages. Justine does mention it should work with Fortran - can anyone give me some instruction? Here are the missing routines mentioned in the error messages:

\_gfortran\_st\_write  
\_gfortran\_transfer\_character\_write  
\_gfortran\_st\_write\_done  
\_gfortran\_stop\_string  
\_gfortran\_set\_args  
\_gfortran\_set\_options

The Fortan source was just:

```
  write(*,*) 'Hello,world'
  stop
  end

```

so no need for options or arguments, but certainly some I/O is needed.

---

<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, 2022, 9:44pm UTC](https://fortran-lang.discourse.group/t/actually-portable-executable/4764/2 "2022-11-18T21:44:43Z")

</div>

The command line for C needs some extra commands to work, _i.e._ to link against the cosmopolitan libc rather than glibc / musl. What command are you using for linking the Fortran binary?

From the discussion around making APEs for C++ it would require a C++ stdlib based on the cosmopolitan libc, I guess the same applies for the GFortran runtime?

---

<div class="post-metadata">

**Author:** ![feenberg](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@feenberg](https://fortran-lang.discourse.group/u/feenberg)\
**Post date:** [November 18, 2022, 10:57pm UTC](https://fortran-lang.discourse.group/t/actually-portable-executable/4764/3 "2022-11-18T22:57:47Z")

</div>

I am on FreeBSD13.0-RELEASE with gcc at version 11.3.0. The compile and link command is:  
gcc -g -O -static -fno-pie -mno-red-zone -nostdlib   
-nostdinc -o hello.exe hello.for -Wl,–oformat=binary   
-Wl,–gc-sections -Wl,-z, -max-page-size=0x1000 -fuse-ld=bfd   
-gdwarf-4 -Wl,-T,ape.lds crt.o ape.o cosmopolitan.a -no-pie

I think you are suggesting I add libgfortran.a to the list of libraries, which makes sense. I suppose I didn’t do that right, because it left me with hundreds of unresolved references. I don’t have any experience with specifying partiular libraries - can you help?

I use the exact same command for C, only the hello.for would be hello.c. No other changes, and that does work.

I also tried Linux, that system didn’t accept the -fno-pie option, and if I removed that gcc couldn’t find WinMain.

Thanks  
Daniel Feenberg
