C- Fortran interface - Variable number of arguments 
Author Message
 C- Fortran interface - Variable number of arguments

Hi
I am facing a problem while calling a C function with variable number
of arguments from fortran.   Since the C function has variable number
of arguments and also the range of arguments is not fixed it might be
difficult task to create a wrapper (although in C) to enable
callability from Fortran.
My C function is defined as

void addMessage(int msgNo, ...)
Here the three dots after the first mandatory argument is an essential
syntax for C variable arguments. Right now i am in a fix as to how to
go abt writing a wrapper for fortran. Any inputs or suggestions in
this regard are most welcome.
Regards
Arun



Tue, 27 Oct 2009 17:23:37 GMT  
 C- Fortran interface - Variable number of arguments

Quote:
> Hi
> I am facing a problem while calling a C function with variable number
> of arguments from Fortran.   Since the C function has variable number
> of arguments and also the range of arguments is not fixed it might be
> difficult task to create a wrapper (although in C) to enable
> callability from Fortran.
> My C function is defined as

> void addMessage(int msgNo, ...)
> Here the three dots after the first mandatory argument is an essential
> syntax for C variable arguments. Right now i am in a fix as to how to
> go abt writing a wrapper for fortran. Any inputs or suggestions in
> this regard are most welcome.
> Regards
> Arun

Fortran can not deal with such routines as you know and in C you
have to use special mechanisms (the varargs macros or whatever they
are called) to deal with variable-length argument lists.

How does your C routine know how many arguments were passed, is
that the msgNo argument? What kind of arguments are passed?

You could do it in this way:
- If they are all the same type, then pass them as an array in Fortran
  and use a C function to split them into separate arguments:

  addMessageF( int msgNo, int array[] ) {
       switch (msgNo) {
       case 1:
            addMessage( 1, array[0] ) ;
            break;
       case 2:
            addMessage( 2, array[0], array[1] );
            break;
       case 3:
            ..
       default:
            /* Error message */
       }
  }

Given that the interface does not have a format string or something
similar, my guess is the above solution is what you could use.

If there is a variety of argument types (i.e. the first can be an
integer,
the second a string or vice versa), you could use a derived type to
store all possibilities and pass an array of derived types to the
C wrapper. (Complicating factor is of course the fact that Fortran
derived types are not easily/portably dealt with in C)

Regards,

Arjen



Tue, 27 Oct 2009 17:53:40 GMT  
 C- Fortran interface - Variable number of arguments
(snip on C varargs functions)

Quote:
> Fortran can not deal with such routines as you know and in C you
> have to use special mechanisms (the varargs macros or whatever they
> are called) to deal with variable-length argument lists.

C allows varargs routines do use a different calling mechanism,
but many use the same calling mechanism if it works.  With the
popular 'push all the arguments on the stack' method, it should
work fine.  Many now pass some arguments in registers, and in
some cases the varargs routine then push them onto the stack so
they can be indexed as before.

Assuming that the calling method are compatible and that the
Fortran compiler doesn't refuse to compile them, it could work.

-- glen



Tue, 27 Oct 2009 20:15:55 GMT  
 C- Fortran interface - Variable number of arguments
| Hi
| I am facing a problem while calling a C function with variable number
| of arguments from Fortran.   Since the C function has variable number
| of arguments and also the range of arguments is not fixed it might be
| difficult task to create a wrapper (although in C) to enable
| callability from Fortran.
| My C function is defined as
|
| void addMessage(int msgNo, ...)
| Here the three dots after the first mandatory argument is an essential
| syntax for C variable arguments. Right now i am in a fix as to how to
| go abt writing a wrapper for fortran. Any inputs or suggestions in
| this regard are most welcome.
| Regards
| Arun

See here:

http://softwarecommunity.intel.com/isn/Community/en-US/forums/thread/...

I'm fairly sure it isn't portable, but you can give it a try.

--
 Jugoslav
___________
www.xeffort.com

Please reply to the newsgroup.
You can find my real e-mail on my home page above.



Tue, 27 Oct 2009 21:08:27 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. passing functions with variable number of arguments as procedure arguments

2. Argument parsing in C module with variable number of arguments

3. Interfacing to C variable length argument s

4. Variable argument functions as an interface.

5. Eiffel cleanup #4: Routines with variable numbers of arguments

6. Variable number of arguments

7. Variable number of arguments to procedures

8. Syntax for variable numbers of arguments

9. define-syntax and variable numbers of arguments

10. Variable Numbers of Arguments ?

11. variable number of arguments ?

12. Can you call a C routine with a variable number of arguments from

 

 
Powered by phpBB® Forum Software