public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* FW: How do I link a C function with a gFortran program
@ 2007-01-02 11:31 Sherman
  2007-01-02 12:41 ` Tim Prince
  0 siblings, 1 reply; 3+ messages in thread
From: Sherman @ 2007-01-02 11:31 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: FW: How do I link a C function with a gFortran program
  2007-01-02 11:31 FW: How do I link a C function with a gFortran program Sherman
@ 2007-01-02 12:41 ` Tim Prince
  0 siblings, 0 replies; 3+ messages in thread
From: Tim Prince @ 2007-01-02 12:41 UTC (permalink / raw)
  To: Sherman; +Cc: gcc-help

Sherman wrote:
> 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?
> 

g77 doesn't have an option AFAIK to produce mixed case link symbols.  As 
your report shows clearly, gfortran follows the Fortran standard in 
making it default to all lower case.  If you want to be compatible with 
past released versions of gfortran, you must go along with this. Thus 
the frequent use of C compilations like
gcc -c -DRead_Block=read_block Read_Block.c

Probably by the end of this year, many Fortran compilers will implement 
the syntax (not tested):

      integer function Read_Block(fnp, fp, fb) bind(c, name='Read_Block')

As you are probably aware, many Fortran compilers for Windows have 
Windows-specific features for this.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* FW: how do I link a C function with a gfortran program
@ 2007-01-02 19:10 jimmie.davis
  0 siblings, 0 replies; 3+ messages in thread
From: jimmie.davis @ 2007-01-02 19:10 UTC (permalink / raw)
  To: gcc-help; +Cc: shermand

The convention is that all fortran subroutines have an underscore at the
end.

In your C file, declare it as long read_block_, and in your fortran code
call it 'read_block'.

->Both program and function compile, but when I try to link, I get the
->message,  "undefined reference to 'read_block_'."


The linker is asking for a symbol called read_block_. Not read_block.


HTH,
bud

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-01-02 19:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-02 11:31 FW: How do I link a C function with a gFortran program Sherman
2007-01-02 12:41 ` Tim Prince
2007-01-02 19:10 FW: how do I link a C function with a gfortran program jimmie.davis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).