This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
FW: How do I link a C function with a gFortran program
- From: "Sherman" <shermand at westnet dot com dot au>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Tue, 2 Jan 2007 22:31:39 +1100
- Subject: FW: How do I link a C function with a gFortran program
I am trying to invoke a C function from a gFortran program.
The C function is called Read_Block and is intended to read one block from a
binary file that was not created by Fortran so does not have the fortran
header and footer on each block.
Both program and function compile, but when I try to link, I get the
message, "undefined reference to 'read_block_'."
If I use the compile switch -fno-underscoring I similarly get "undefined
reference to 'read_block'."
I assume this is a name mangling problem, and I need a switch to tell
gFortran to use C type name mangling.
# Is there a solution?
# If it is not yet available in gFortran, is it available in g77?
Thank you if you can point me to any help.
Doug Sherman
_____________________
Below are my test program, function and makefile.
I am running Windows XP and invoking the makefile from a command window.
READ_DAC.F90
============
program main
interface
integer function Read_Block(fnp, fp, fb)
implicit none
character*60, pointer :: fnp ! file name pointer
integer, pointer :: fp ! FILE pointer
character*512, pointer :: fb ! block pointer
end function Read_Block
end interface
character*60, target :: filename
character*60, pointer :: fnp
integer, pointer :: fp ! FILE pointer
character*512, target :: block
character*512, pointer :: fb ! block pointer
fnp => filename
fp = 0
fb => block
filename = 'OLM_ZH878_255_3_C_V_201.dac' // char(0)
numread = Read_Block (fnp, fp, fb)
if(numread .ne. 512)print *, "numread = ", numread, " != 512 !!"
end program main
READ_BLOCK.C
============
/* Read_Block (fn*, fp*, bp*) Read one block from a .DAC file
* fn is a pointer to a filename
* fp is a pointer to the file.
* bp is a pointer to a block of 512 bytes
* fp must be 0 at first call to function for each new file name.
* In subsequent calls, the value of fp must be the same as that returned
* at the first call for the given file name.
* I'VE JUST REALISED THAT FP WILL -NOT- BE RETURNED THIS WAY.
* (I'M TOO USED TO FORTRAN SUBROUTINES RATHER THAN C!)
* BUT I CAN FIX THAT PROBLEM. THE MAIN PROBLEM ABOUT LINKING STILL
REMAINS.
* Read_Block returns number of bytes read (should be 512, otherwise error,
probably EOF).
* Exits with errorflag of 1 if unable to open file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long Read_Block (char*, FILE*, char*);
long Read_Block (char* fnp, FILE* fp, char* bp) { if ( fp==0 )
{ if ( (fp = fopen(fnp, "rb")) == NULL )
{fprintf(stderr, "Error opening file: %s\n", fnp);
exit(1);
}
}
int numread = fread(bp, sizeof(char), 512, fp);
return numread;
}
MAKEFILE
========
objs = Read_DAC.o Read_Block.o
Read_DAC.exe : $(objs)
gfortran -o Read_DAC.exe $(objs)
Read_DAC.o : Read_DAC.f90
gfortran -c -fbounds-check -fno-backslash -funderscoring Read_DAC.f90
>Read_DAC.err 2>&1
Read_Block.o : Read_Block.c
gcc -c -fbounds-check -Wall Read_Block.c
>Read_Block.err 2>&1