public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Building a program with C an FORTRAN code
@ 1998-10-29 19:35 N8TM
  0 siblings, 0 replies; 5+ messages in thread
From: N8TM @ 1998-10-29 19:35 UTC (permalink / raw)
  To: ramiro, egcs

In a message dated 10/29/98 1:28:25 PM Pacific Standard Time,
ramiro@labtie.mmt.upc.es writes:

> undefined references to
>  the functions writen in FORTRAN (witch are referenced from C)
Without further information, the most obvious possibility is that you have not
reconciled the function-name-mangling schemes of the 2 systems.  The +ppu
option of HP Fortran is closest to the g77 default; there are several g77
options dealing with post-pended underscores.  In either case, nm or objdump
will show the names presented to ld by the .o files.

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

* Re: Building a program with C an FORTRAN code
  1998-10-29  2:23 Ramiro Alba Queipo
  1998-10-29 14:18 ` Dave Love
@ 1998-10-30  9:53 ` Luc Maisonobe
  1998-10-30  9:53 ` Martin Kahlert
  2 siblings, 0 replies; 5+ messages in thread
From: Luc Maisonobe @ 1998-10-30  9:53 UTC (permalink / raw)
  To: egcs

   Ramiro> I am trying to buil a program writen in C wicth has some code in
   Ramiro> FORTRAN. The compiling
   Ramiro> phase has no problems but when linking the objets together, it finds
   Ramiro> undefined references to
   Ramiro> the functions writen in FORTRAN (witch are referenced from C), as if
   Ramiro> there were not defined
   Ramiro> anywhere.

I have used C/C++/fortran combinations for years without
problems. However there are two things to be aware of ; the compiler
can transform function names when building symbols, and the languages
conventions for parameters differ.



I think (I'm not sure) that HP does not transform names for C and
convert names to lowercase for fortran. So the C funtion int foobar ()
can be called from fortran by : N = FOOBAR ().

Another common approach (egcs for example) is to add an underscore
after fortran names. If you want to write N = FOOBAR (), the compiler
will use the name foobar_ and so your C function should be named
int foobar_ ().

Warning : if you want to call a C++ function from fortran, you should
declare it as extern "C" int foobar_ (), otherwise the C++ name
mangling will prevent you from calling it from fortran.

The nm command can be used to find the symbols names of your object
files and to understand the coding used. I think your problem is here,
did you add the underscore ?



Fortran uses references for all parameters, whereas C uses
values. references are generally implemented by addresses and C knows
about addresses.

The solution is to use addresses on the C side :

    fortran side :
        INTEGER          I, N, FOOBAR
        DOUBLE PRECISION X
        N = FOOBAR (I, X)

    C side :
        int foobar_ (int *ptrI, double *ptrX)
        { return (*ptrX > 0.0) ? *ptrI : -*ptrI; }

You can use references if you call a C++ function :
        extern "C" int foobar_ (int &i, double &x)
        { return (x > 0.0) ? i : -i; }

The last problem is with character strings, since there is no end of
string character in fortran. Fortran compilers generally add an extra
argument (the declared length) for each character string after all
explicit arguments. Those extra parameters are generally passed by
value, as in the following example :

    fortran side :
        CHARACTER*80     S1
        CHARACTER*20     S2
        INTEGER          I, N, FOOBAR
        DOUBLE PRECISION X
        N = FOOBAR (S1, I, S2, X)

    C side :
        int foobar_ (char *s1, int *ptrI, char *s2, double *ptrX,
                     int l1, int l2)

    C++ side :
        extern "C" int foobar_ (char *s1, int &i, char *s2, double &x,
                                int l1, int l2)

In the preceding cases, the C and C++ functions would receive 80 for
the extra l1 parameter and 20 for the extra l2 parameter.


All those conventions vary accross systems and compilers, I think egcs
trys to follow the conventions of the native compilers if they are
known, thus allowing to mix egcs code with other compilers code. You
should try and possibly configure your C code with #ifdef if you
intend to port it to several architectures.

                                                    Luc

-- 
------------------------------------------------------------------------------
 Luc Maisonobe - DTS/MPI/MS/AM       |  Tel  : (33) 05-61-28-26-31
            CNES                     |  Fax  : (33) 05-61-27-35-40
     18 avenue E. Belin              |
 31401 Toulouse CEDEX 4 - FRANCE     |  Email: Luc.Maisonobe@cnes.fr
------------------------------------------------------------------------------

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

* Re: Building a program with C an FORTRAN code
  1998-10-29  2:23 Ramiro Alba Queipo
  1998-10-29 14:18 ` Dave Love
  1998-10-30  9:53 ` Luc Maisonobe
@ 1998-10-30  9:53 ` Martin Kahlert
  2 siblings, 0 replies; 5+ messages in thread
From: Martin Kahlert @ 1998-10-30  9:53 UTC (permalink / raw)
  To: Ramiro Alba Queipo; +Cc: egcs

Quoting Ramiro Alba Queipo (ramiro@labtie.mmt.upc.es):

> I am trying to buil a program writen in C wicth has some code in
> FORTRAN. The compiling
> phase has no problems but when linking the objets together, it finds
> undefined references to
> the functions writen in FORTRAN (witch are referenced from C), as if
> there were not defined
> anywhere.

You didn't provide much information, which symbols are not found,
but the fact, that it runs on HP suggests the following:

Under all compilers/OS's i use at work 
(SGI/HP/LINUX/WINNT/SOLARIS/SUN4), 
HP-Fortran is the only one, which doesn't append an 
underscore to function names.

On all other OS's you have to do this:
Fortran:
  SUBROUTINE FOO(...)
  ....
C:
  foo_(....); /* the underscore is important! */
  ....

To investigate these things, use: nm fortran_object_file.o

I would handle it that way: 
create a header file for the Fortran routines and write
#ifdef underscore
#define foo foo_
#define bar bar_
#endif

and then conpile with -Dundsc at the command line.


Fortran provides functions in lowercase letters in the 
object files.
Hint: All Common block-names have an underscore appended, too.

Hope, that helps,
Martin.


-- 
Your mouse has moved. Windows must be restarted for the change
to take effect. Reboot now?

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

* Re: Building a program with C an FORTRAN code
  1998-10-29  2:23 Ramiro Alba Queipo
@ 1998-10-29 14:18 ` Dave Love
  1998-10-30  9:53 ` Luc Maisonobe
  1998-10-30  9:53 ` Martin Kahlert
  2 siblings, 0 replies; 5+ messages in thread
From: Dave Love @ 1998-10-29 14:18 UTC (permalink / raw)
  To: Ramiro Alba Queipo; +Cc: egcs

The g77 manual contains information on linking C and Fortran.  If that
isn't enough, could you tell us what else you need to know?

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

* Building a program with C an FORTRAN code
@ 1998-10-29  2:23 Ramiro Alba Queipo
  1998-10-29 14:18 ` Dave Love
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Ramiro Alba Queipo @ 1998-10-29  2:23 UTC (permalink / raw)
  To: egcs

Hello everybody:

I am trying to buil a program writen in C wicth has some code in
FORTRAN. The compiling
phase has no problems but when linking the objets together, it finds
undefined references to
the functions writen in FORTRAN (witch are referenced from C), as if
there were not defined
anywhere.

The development environment is:

HARDWARE        :               Intel Pentium Clasic 166 Mhz
S.O.:                          :               Debian/Gnu Linux 2.0
EGCS:                     :               1.0.3 release
LINUX KERNEL:                2.0.34

The same code is working fine in HP-UX 10.20 built by the HP compilers.

Please, when doing a reply send me a copy, as I am not subscribed to
this maling list

Thanks in advance

--
Ramiro Alba
Laboratori de Termotecnia i Energetica

Departament de Maquines i Motors Termics
ETS d'Enginyers Industrials de Terrassa

C/Colom 11

Tf: 34 - 93 739 82 43
Fax: 34 - 93 739 81 01

e-mail: ramiro@labtie.mmt.upc.es




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

end of thread, other threads:[~1998-10-30  9:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-10-29 19:35 Building a program with C an FORTRAN code N8TM
  -- strict thread matches above, loose matches on Subject: below --
1998-10-29  2:23 Ramiro Alba Queipo
1998-10-29 14:18 ` Dave Love
1998-10-30  9:53 ` Luc Maisonobe
1998-10-30  9:53 ` Martin Kahlert

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).