Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* Mixed language programming
@ 1994-08-01 14:37 Phil Neal
  0 siblings, 0 replies; 4+ messages in thread
From: Phil Neal @ 1994-08-01 14:37 UTC (permalink / raw)




Dear osf/alpha people,

Does anyone have refereces to or examples of 
mixed language programming on alphas. In particular,
I am looking for examples in Fortran and C of passing
arrays, scalars, and strings (is there anything else in life ?).

Thanks,

Phil



----------------------------------------------------------------
Phil Neal, Systems Programmer, Statistics Department GN-22
University of Washington, Seattle, Wa. 98195 USA  | 206-685-1627
http://www.stat.washington.edu/phil.html
----------------------------------------------------------------
Those who would repeat the past must control the teaching of history.
(Bene Gesserit Coda)


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

* Mixed language programming
@ 1994-08-01 22:35 Phil Neal
  1994-08-02  6:59 ` system PRIVILEGED account
  1994-08-02 14:02 ` Alexander Kovzelev
  0 siblings, 2 replies; 4+ messages in thread
From: Phil Neal @ 1994-08-01 22:35 UTC (permalink / raw)




Dear osf1/alpha people,

I have been having a problem trying to make a program with
a C main and a f77 subroutine. I have been through the man
pages and the dxbook stuff for f77. It gives examples for
f77 calling C but never C calling f77. Any pointers would
be extremely appreciated (no pun intended).


Here is the main:

extern testit_();
main()
{
testit_(); 
printf(" back again \n");
}

And here is the subroutine:


        subroutine testit()
        write(*,*) ' in testit'
        return
        end

Here is the make file:

test.exe:       test.o testit.o
        f77 -v test.o  testit.o -o test.exe
clean:
        rm *.o *.exe


Here is the error message:

_trieste_215% make
cc  -c test.c
f77  -c testit.f
f77 -v test.o  testit.o -o test.exe
/usr/bin/cc -v -o test.exe /usr/lib/cmplrs/fort/for_main.o -O4
test.o testit.o -nocount -lUfor -lfor -lFutil -lm -lots 
/usr/lib/cmplrs/cc/ld -o test.exe -g0 -O4 -call_shared
/usr/lib/cmplrs/cc/crt0.o /usr/lib/cmplrs/fort/for_main.o test.o
testit.o -nocount -lUfor -lfor -lFutil -lm -lots -lc 
ld:
test.o: main: multiply defined
Unresolved:
MAIN__
0.0u 0.0s 0:00 60% 0+8k 3+5io 22pf+0w
fort: Severe: Failed while trying to link.
*** Exit 1
Stop.


Phil



----------------------------------------------------------------
Phil Neal, Systems Programmer, Statistics Department GN-22
University of Washington, Seattle, Wa. 98195 USA  | 206-685-1627
http://www.stat.washington.edu/phil.html
----------------------------------------------------------------
Those who would repeat the past must control the teaching of history.
(Bene Gesserit Coda)


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

* Re: Mixed language programming
  1994-08-01 22:35 Phil Neal
@ 1994-08-02  6:59 ` system PRIVILEGED account
  1994-08-02 14:02 ` Alexander Kovzelev
  1 sibling, 0 replies; 4+ messages in thread
From: system PRIVILEGED account @ 1994-08-02  6:59 UTC (permalink / raw)


Phil Neal (neal@trieste.stat.washington.edu) wrote:

: Dear osf1/alpha people,

: I have been having a problem trying to make a program with
: a C main and a f77 subroutine. I have been through the man
: pages and the dxbook stuff for f77. It gives examples for
: f77 calling C but never C calling f77. Any pointers would
: be extremely appreciated (no pun intended).


: Here is the main:

: extern testit_();
: main()
: {
: testit_(); 
: printf(" back again \n");
: }

: And here is the subroutine:


:         subroutine testit()
:         write(*,*) ' in testit'
:         return
:         end


I tried the other way:

          >f77 -c testit.f
          >cc test.c testit.o  -lfor
          >a.out
            in testit
           back again
          >

and it worked fine (OSF V2.0).

Michael Polnick
TU DRESDEN



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

* Re: Mixed language programming
  1994-08-01 22:35 Phil Neal
  1994-08-02  6:59 ` system PRIVILEGED account
@ 1994-08-02 14:02 ` Alexander Kovzelev
  1 sibling, 0 replies; 4+ messages in thread
From: Alexander Kovzelev @ 1994-08-02 14:02 UTC (permalink / raw)


Phil Neal (neal@trieste.stat.washington.edu) wrote:


> Dear osf1/alpha people,

> I have been having a problem trying to make a program with
> a C main and a f77 subroutine. I have been through the man
> pages and the dxbook stuff for f77. It gives examples for
> f77 calling C but never C calling f77. Any pointers would
> be extremely appreciated (no pun intended).


> Here is the main:

> extern testit_();
> main()
> {
> testit_(); 
> printf(" back again \n");
> }

> And here is the subroutine:


>         subroutine testit()
>         write(*,*) ' in testit'
>         return
>         end

> Here is the make file:

> test.exe:       test.o testit.o
>         f77 -v test.o  testit.o -o test.exe
> clean:
>         rm *.o *.exe


> Here is the error message:

> _trieste_215% make
> cc  -c test.c
> f77  -c testit.f
> f77 -v test.o  testit.o -o test.exe
> /usr/bin/cc -v -o test.exe /usr/lib/cmplrs/fort/for_main.o -O4
> test.o testit.o -nocount -lUfor -lfor -lFutil -lm -lots 
> /usr/lib/cmplrs/cc/ld -o test.exe -g0 -O4 -call_shared
> /usr/lib/cmplrs/cc/crt0.o /usr/lib/cmplrs/fort/for_main.o test.o
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
That's the reason of your problems, the file include `main' function
which does f77 real-time library initialisation and call `MAIN__'
which is the name of fortran main program. 

> testit.o -nocount -lUfor -lfor -lFutil -lm -lots -lc 
> ld:
> test.o: main: multiply defined
> Unresolved:
> MAIN__
> 0.0u 0.0s 0:00 60% 0+8k 3+5io 22pf+0w
> fort: Severe: Failed while trying to link.
> *** Exit 1
> Stop.

> Phil

There are two ways to solve the problem:

1. Just link with `cc', but add f77 libraries:
   cc -o test.exe test.o testit.o -lUfor -lfor -lFutil -lm -lots
  Advantage: simplisity, you can link with C++ code as well.
  Disadvantages: Calls iargc(3f), getarg(3f) will not work (they need
  initialisation in `for_main.o').  You should state explicitly fortran
  libraries. THERE IS POSSIBILITY THAT SOME OF FORTRAN I/O WILL NOT
  WORK PROPERLY. I have never seen source for f77 run time library
  so I don't know what kind of initialisation `for_main' make. From
  my own expirience sequential formatted I/O works fine.

2. Let dummy fortran `PROGRAM DUMMY' calls 'SUBROUTINE CMAIN' and 
  rename your main C program from `main' to `cmain_'.
  Advantages: Do f77 run-time initialisation so all fortran code SHOULD 
  works.
  Disadvantages: A hack is a hack: C++ static objects constructors
  are not called. If you are using g++ add `__main()' call in the very
  beginning of `cmain_' to call the constructors. One needs special code
  if you want to handle  command arguments in a usual C way.
 
Here is an example of passing command arguments from fortran to C, it has
two flaws: Don't try to pass an argment more than 100 characters long,
I get my old code and generalized it a little bit so test before use.

----- cut here main.f ----
      program MAIN
      character*100 opts(100)
c
      do i=0,iargc()
        call getarg(i,opts(i+1))
      enddo
c
      call cstart(iargc()+1,opts)
      end
----- cut here cstart.c ----
/*
 * This code gets argc argv from Fortran Main,  process it for C
 * and call cmain_
 */
#ifdef __GNUC__
#define alloca __builtin_alloca
#else
#include <alloca.h>
#endif

lenocc_(char *string, int len);

int
cstart_(int *argc, char *argv, int len)
{
  int i, lenocc;
  char **av = (char **)alloca(sizeof(char*) * (*argc+1));

  for (i=0; i < *argc; i++) {
    if ((lenocc=lenocc_(&(argv[i*len]),len)) == len) {
      av[i] = alloca(lenocc+1);
      strncpy(av[i],&(argv[i*len]),len);
      av[i][len+1] = '\0';
    } else {
      av[i] = &(argv[i*len]);
      av[i][lenocc] = '\0';
    }
  }
  av[i] = (char*)0;
 
#ifdef __GNUC__
  __main()
#endif
  return cmain_(*argc,av);
}

/*
 * This function returns the actual lenght of Fortran string.
 * (trailing spaces are skipped).
 *
 * There is straighforward and slow implementation.
 */
lenocc_(char *string, int len)
{
  while (string[--len] == ' ');
  return len;
}
----- cut here: the end ----

--
  Alexander Kovzelev,      Institute for Nuclear Research, Moscow, Russia
  e-mail:   Kovzelev@mx.ihep.su (In Russia)
	    Kovzelev@cern.ch    (In Switzerland) <- Now I am here



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

end of thread, other threads:[~1994-08-02 14:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1994-08-01 14:37 Mixed language programming Phil Neal
  -- strict thread matches above, loose matches on Subject: below --
1994-08-01 22:35 Phil Neal
1994-08-02  6:59 ` system PRIVILEGED account
1994-08-02 14:02 ` Alexander Kovzelev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox