Re: How to set my own program startup in f77 programs other than MAIN__
- From: Chip Coldwell <coldwell@xxxxxxxxxxxxx>
- Date: Fri, 26 Oct 2007 10:51:13 -0400
ravi <gotothati@xxxxxxxxx> writes:
On Oct 26, 12:01 am, Chip Coldwell <coldw...@xxxxxxxxxxxxx> wrote:
ravi <gototh...@xxxxxxxxx> writes:
On Oct 17, 10:42 pm, Chip Coldwell <coldw...@xxxxxxxxxxxxx> wrote:
ravi <gototh...@xxxxxxxxx> writes:
yes,I would like to execute my C routine foo() for every fortran
program.
As the program execution starts with MAIN__ function, i thought of
#defining the MAIN__ symbol. But we don't have the source file where
MAIN__ symbol located actually.
I would give the user a library which has to be linked to the FORTRAN
program while generating the executable,and i have permissions to give
user a header file to define needed symbols or routines.
OK, just to tie up the remaining loose ends:
$ cat test.f
PROGRAM TEST
WRITE (6,'(a22)') 'Hello, world from test'
END
$ cat foo.c
#include <stdio.h>
typedef void (*func_ptr) (void);
void foo__(void)
{
puts("Hello, world from foo__");
}
func_ptr __attribute__ ((__section__ (".ctors"))) foo__ctor = foo__;
$ gcc -c foo.c
$ ld -r -o libfoo.so foo.o
$ g77 -o test test.f -L. -lfoo
$ ./test
Hello, world from foo__
Hello, world from test
I think that meets all your requirements. It works for g77, gfortran
and ifort (using either gcc or icc to compile foo.c). It does not
work with sunf77/sunf95, so this behavior is still compiler dependent,
even if you stick to Linux.
Chip
Thanks a lot for that solution.
But it won't even portable on AIX XL compiler.
How to get a Portable solution ? (My user may be Linux,Solaris,or
AIX)
Ah ha! Now the truth comes out. In your original posting you said
I am writing simple F77 program compiler g77 on Linux.
So my solution does meet the original requirements, but now you've
changed them! Why not throw in Cray HPF while you're at it ;->
On Solaris, I think it is sufficient to do this
$ cat foo.c
#include <stdio.h>
#pragma init(foo__)
void foo__(void)
{
puts("Hello, world from foo__");
}
I believe that will work for both gcc on Solaris and the Sun Forte (or
whatever they call it these days) compiler. Note that gcc on
Linux/x86 will ignore this pragma (and issue a warning if you invoke
it with the -Wall switch).
AIX doesn't even use ELF format binaries (it uses XCOFF, not much
different from a.out), so I'm really not sure what you could do for
that. I think the whole approach of stuffing your initializer into a
special binary section goes out the window.
Chip
--
Charles M. "Chip" Coldwell
"Turn on, log in, tune out"
Somerville, Massachusetts, New England
How about LD_PRELOAD for intercepting main ??
I have not used this hijacking previously.
That's probably not the right thing to do unless you know what the IBM
XL compiler does to set up the Fortran run-time. In the g77 example I
gave before, we do know exactly what it does (since it's in the
documentation and the source code is available). Without this
information, you run the risk of having a Fortran run-time that is not
properly initialized.
There is some tempting wording in the IBM XL documentation online:
"Operation of the Runtime Linker
The main program is loaded and resolved by the system loader in the
usual way. If the executable program cannot be loaded for any reason,
the exec() subroutine fails and the runtime linker is not invoked at
all. If the main program loads successfully, control passes to the
runtime linker, which rebinds symbols as described below. When the
runtime linker completes, initialization routines are called, if
appropriate, and then the main function is called."
So it sounds like it should be possible to define an intialization
routine. I don't have an AIX box handy (or Solaris for that matter)
to try anything, though.
Chip
--
Charles M. "Chip" Coldwell
"Turn on, log in, tune out"
Somerville, Massachusetts, New England
.
- Follow-Ups:
- Re: How to set my own program startup in f77 programs other than MAIN__
- From: Chip Coldwell
- Re: How to set my own program startup in f77 programs other than MAIN__
- References:
- How to set my own program startup in f77 programs other than MAIN__
- From: ravi
- Re: How to set my own program startup in f77 programs other than MAIN__
- From: Richard Maine
- Re: How to set my own program startup in f77 programs other than MAIN__
- From: ravi
- Re: How to set my own program startup in f77 programs other than MAIN__
- From: Chip Coldwell
- Re: How to set my own program startup in f77 programs other than MAIN__
- From: ravi
- Re: How to set my own program startup in f77 programs other than MAIN__
- From: Chip Coldwell
- Re: How to set my own program startup in f77 programs other than MAIN__
- From: ravi
- How to set my own program startup in f77 programs other than MAIN__
- Prev by Date: Re: Host association of module ENTRYs
- Next by Date: Re: Host association of module ENTRYs
- Previous by thread: Re: How to set my own program startup in f77 programs other than MAIN__
- Next by thread: Re: How to set my own program startup in f77 programs other than MAIN__
- Index(es):
Relevant Pages
|