2

I am trying to get a 'hello world' type program compiled in FreeBSD 12 with flang.

This is my source code:

PROGRAM MAIN
  INTEGER :: X

  PRINT *, "Please, enter a number"
  READ (*, *) X
  PRINT *, "The square root of ", X, " is ", SQRT(X)
END PROGRAM MAIN

I try to compile it without success using:

$ flang -o test test.f90
/usr/local/bin/ld: /tmp/test-8e54ee.o: in function `MAIN_':
/usr/home/user/test/test.f90:6: undefined reference to `sqrt_'
/usr/local/bin/ld: /usr/local/flang/lib/libflangrti.so: undefined reference to `backtrace_symbols'
/usr/local/bin/ld: /usr/local/flang/lib/libflangrti.so: undefined reference to `backtrace'
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)

It has been a long time since last time I used Fortran and it is definitively the first time I try to compile it with FreeBSD. Any help/hint is welcomed.

M.E.
  • 4,955
  • 4
  • 49
  • 128
  • 2
    Don't use flang. Install gfortran and be happy. – evets Sep 07 '19 at 04:58
  • @evets as FreeBSD does not use gcc but clang I thought flang is the natural way to use Fortran. I am definitively looking to find a way to use Fortran in FreeBSD, if I install gcc, wouldn’t that interfere with clang? – M.E. Sep 07 '19 at 12:02
  • gcc does not interfere with clang. Install ports/lang/gcc8 or ports/lang/gcc9. You'll get either gfortran8 or gfortran9. The number is the major version number. I have versions 6, 7, 8, 9, and trunk on my system. All work fine. – evets Sep 07 '19 at 13:27
  • @M.E. `flang` was an early attempt at making a Fortran compiler for LLVM. It has been abandoned long ago and should not be removed. – fuz Nov 12 '19 at 14:18

1 Answers1

1

I was able to get around the first problem ("undefined reference to sqrt_") by declaring X as

REAL :: X

This makes sense, because SQRT in Fortran is defined for real numbers (otherwise it is not clear what KIND the returned REAL result would have), so flang does not resolve the call and expects that it is a reference to your own custom function defined somewhere else in the code.

As for the second problem ("undefined reference to backtrace_symbols"), this seems to me as a mess in installation. I have just installed a clean FreeBSD 12 into VirtualBox and the linker is in "/usr/bin/ld", and this is where it is looked for by flang, as apparent from the verbose output:

$ flang -o test test.f90
(...)
 "/usr/bin/ld" --eh-frame-hdr -dynamic-linker /libexec/ld-elf.so.1 (... etc ...)
jacob
  • 1,535
  • 1
  • 11
  • 26
  • Thanks for the answer @jacob, regarding the second issue, I did not understand if you are suggesting that my installation might be messed up or it is FreeBSD 12 the one that does not work. I cand find `ld` in `/usr/loca/bin/ld`, `/usr/local/x86_64-portbld-freebsd12.0/bin/ld` and `/usr/bin/ld`. – M.E. Sep 07 '19 at 15:18
  • I mean that a clean fresh installation of FreeBSD 12 does not seem to have `ld` elsewhere than in `/usr/bin/ld`. So it seems that you have something extra installed (maybe GNU `ld`?), which clashes with the system LLVM `ld`. – jacob Sep 07 '19 at 15:26
  • It seems I already got gcc8 installed, and it seems it is a requirement for py36 (which I need) – M.E. Sep 07 '19 at 20:31