using iargc()/getarg() form C
Satish Balay
balay@fastmail.fm
Wed Feb 23 04:01:00 GMT 2005
On Tue, 22 Feb 2005, Steve Kargl wrote:
> > Could you tell me what are the correct symbols in the fortran
> > library that I can call directly from C?
>
> I just looked at the implementation of iargc. You can't
> get at the command line arguments with the gfortran
> functions from a C program. Well, you might be able to
> get them, but we need more details in exactly what
> you're doing.
Ok - here is the usage with gcc3
##########################################
# cat arg.c
#include <stdio.h>
extern int iargc_();
extern void getarg_(int*,char*,int);
void foo_()
{
int num, len, status;
char name[25];
num = 0;
getarg_(&num,&name[0],25);
name[24]=0;
printf("Program name: %s\n",name);
num = iargc_();
printf("No of args: %d\n",num);
}
# cat arg-test.f
program main
call foo()
end
# gcc -c arg.c
# g77 arg-test.f arg.o -o arg-test
# ./arg-test option1 option2
Program name: ./arg-test
No of args: 2
#
##########################################
This usage works with gcc4 with the following change:
iargc_ -> _gfortran_iargc
getarg_ -> _gfortran_getarg_i4
# gcc -c arg.c
# gcc4 -c arg.c
# gfortran arg-test.f arg.o -o arg-test
# ./arg-test option1 option2
Program name: ./arg-test
No of args: 3
#
[ I guess the off by 1 issue is fixed in CVS ]
[with gcc version 4.0.0 20041019 (Red Hat 4.0.0-0.8)]
##########################################
I guess I could just use the above and be done with it.
But now that 'command_argument_count() & get_command_argument()' are
standerdised - perhaps I should use them? If so, maybe my code will be
cleaner and will work across all compilers that implement this
standard with less mess than the current status quo [which is special
code for almost all compilers]
to check it out I tried:
# cat arg.f
program test
integer num, len, status,cmd_count
character*7 value
num = 0
call GET_COMMAND_ARGUMENT(num, value, len, status)
print*, value
print*, len
print*, status
cmd_count = COMMAND_ARGUMENT_COUNT()
print*, cmd_count
end
# gfortran arg.f
# gfortran -c arg.f
# nm -o arg.o
arg.o: U _gfortran_filename
arg.o: U _gfortran_get_command_argument_i4
arg.o: U _gfortran_iargc
arg.o: U _gfortran_ioparm
arg.o: U _gfortran_line
arg.o: U _gfortran_st_write
arg.o: U _gfortran_st_write_done
arg.o: U _gfortran_transfer_character
arg.o: U _gfortran_transfer_integer
arg.o:00000000 T MAIN__
Obviously I'm looking for some symbols with names
'get_command_argument' & 'command_argument_count but' I don't see
that. [somehow '_gfortran_iargc' crept in]. So this interface doesn't
looks easy to use for my purpose.
> Most C programs have
>
> int main(int argc, char *argv[])
>
> as the entry point. Is there some reason you can't just
> use the usual C idiom.
This kind of usage is found in libraries that provide c/fortran
interfaces - but do most of the internal stuff in C.
Satish
More information about the Fortran
mailing list