What library to link against when doing mixed language C, C++ and Fortran programming

I assume that I need to add a library to the compile stage in the following example.

C calling Fortran

2 d arrays

nvfortran -O4 -c ch3512.f90 -o ch3512_f.o
pgcc -std=c18 ch3512.c ch3512_f.o -o ch3512.out
ch3512.c:
“ch3512.c”, line 8: warning: use of a const variable in a constant expression is nonstandard in C
int x[nr][nc];
^

“ch3512.c”, line 8: warning: use of a const variable in a constant expression is nonstandard in C
int x[nr][nc];
^

“ch3512.c”, line 9: warning: use of a const variable in a constant expression is nonstandard in C
int rsum[nr];
^

“ch3512.c”, line 10: warning: use of a const variable in a constant expression is nonstandard in C
int csum[nc];
^

/usr/bin/ld: ch3512_f.o: in function sums': /home/ian/document/fortran/5th_edition/ch3512.f90:1: undefined reference to pgf90_auto_alloc04_i8’
/usr/bin/ld: /home/ian/document/fortran/5th_edition/ch3512.f90:12: undefined reference to pgf90_mcopy4' /usr/bin/ld: /home/ian/document/fortran/5th_edition/ch3512.f90:14: undefined reference to pgf90_alloc04a_i8’
/usr/bin/ld: /home/ian/document/fortran/5th_edition/ch3512.f90:14: undefined reference to pgf90_dealloc03a_i8' /usr/bin/ld: /home/ian/document/fortran/5th_edition/ch3512.f90:15: undefined reference to pgf90_auto_dealloc_i8’

Here is the fortran code

subroutine sums(nr, nc, x, rsum, csum) bind (c, name=‘sums’)
! gcc requires -lgfortran
use iso_c_binding
implicit none
integer (c_int), value :: nr
integer (c_int), value :: nc
integer (c_int), dimension (1:nr, 1:nc), intent (in) :: x
integer (c_int), dimension (1:nr), intent (out) :: rsum
integer (c_int), dimension (1:nc), intent (out) :: csum
integer (c_int), dimension (1:nc, 1:nr) :: t

t = reshape(x, (/nc,nr/) )
rsum = sum(t, dim=1)
csum = sum(t, dim=2)
end subroutine

here is the c code

include <stdio.h>
void sums(int nr,int nc,int x[nr][nc],
int * rsum, int * csum);
int main()
{
const int nr=2;
const int nc=6;
int x[nr][nc];
int rsum[nr];
int csum[nc];
int r;
int c;
int i=1;
for (r=0;r<nr;r++)
rsum[r]=0;
for (c=0;c<nc;c++)
csum[c]=0;
for (r=0;r<nr;r++)
for (c=0;c<nc;c++)
{
x[r][c]=i;
i++;
}
printf(" C calling Fortran\n");
printf(" 2 d array as parameter\n");
printf(" c99 vla\n");
for (r=0;r<nr;r++)
{
for (c=0;c<nc;c++)
{
printf(" %3d " , x[r][c]);
}
printf(“\n”);
}
printf(“\n”);
sums(nr,nc,x,rsum,csum);
for (r=0;r<nr;r++)
{
for (c=0;c<nc;c++)
{
printf(" %3d " , x[r][c]);
}
printf(" %3d “,rsum[r]);
printf(”\n");
}
printf(“\n”);
for (c=0;c<nc;c++)
printf(" %3d “,csum[c]);
printf(”\n");
return(0);
}

Ian Chivers

Hi Ian,

Please add “-fortranlibs” to include the Fortran runtime libraries.

-Mat

Magic.

the examples now compile and run.