Based on this SO post I want to create software with gcc
which uses mixed language source code, e.g. C
and Fortran
. While the linking of the generated object files works fine, the linker gives me the following error if I use a static or shared library instead of the object file:
/usr/bin/ld: /tmp/ccdqJ6hY.o: in function `MAIN__':hello.f90:(.text+0x76): undefined reference to `fun'collect2: error: ld returned 1 exit status
This is my C library source code file fun.c
:
#include <stdio.h>void fun(void) { printf("Hello from C function.\n");}
This is my Fortran source code hello.f90
:
program hello print *, "Hello from Fortran program." call fun()end
This is my build script:
#! /bin/sh#gcc -c fun.c # Create object file of C libraryar r libfun.a fun.o # Create static C library gcc -shared -o libfun.so fun.o # Create shared/dynamic linked C library#gfortran -fno-underscoring -o hello1 fun.o hello.f90 # This works.#gfortran -fno-underscoring -o hello2 -L. -Wl,-rpath=`pwd` -lfun hello.f90 # This fails.#gfortran -fno-underscoring -o hello3 -L. libfun.a hello.f90 # This fails.
The symbol name fun
is fine in the libraries as indicated by nm
:
$ nm libfun.afun.o:0000000000000000 T fun
I'm using gcc
version gcc (Debian 10.2.1-6) 10.2.1 20210110
on Debian GNU/Linux 11 (bullseye).
I suppose that something with my gcc
configuration might be broken, but I have no glue, what.Could you give me any advice, please?