From: kovzelev@cern.ch (Alexander Kovzelev)
Subject: Re: Mixed language programming
Date: Tue, 2 Aug 1994 14:02:24 GMT [thread overview]
Message-ID: <Ctwv01.4nL@news.cern.ch> (raw)
In-Reply-To: 31jtb4$f1a@news.u.washington.edu
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
next prev parent reply other threads:[~1994-08-02 14:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1994-08-01 22:35 Mixed language programming Phil Neal
1994-08-02 6:59 ` system PRIVILEGED account
1994-08-02 14:02 ` Alexander Kovzelev [this message]
-- strict thread matches above, loose matches on Subject: below --
1994-08-01 14:37 Phil Neal
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox