Actually Portable Executable

At 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:


The Fortan source was just:

  write(*,*) 'Hello,world'

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

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?

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, 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.

Daniel Feenberg