Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call C function from Fortran 1

Status
Not open for further replies.

zhoubinbin

Technical User
Jun 14, 2001
2
US
Hi, anybody can help me!!

I want to call a C function from a Fortran code, For example, I have a C function void ABC(int *x) saved in C_file.c, I want to call this ABC from a Fortran code, such as

Integer X
Call ABC_(X)

which is in F_file.f

So How to link and compile and run this
Fortran code.

Thanks

Binbin


 
The details of how to do this depend on the system and compilers you are using, but in general you would compile the C and Fortran files into separate objects and link them in exactly the same way as you would link 2 objects created with either C or Fortran. Some systems append an underscore to the end of Fortran subroutine names so in your example you may need to declare your C subroutine

void ABC_(int *x)

and call it in Fortran

call ABC(x)

If you are going to port this code to several systems it is a good idea to use defines in a C header file to handle this problem e.g.

#ifdef SGI
#define ABC ABC_
#endif
void ABC(int *x)

Also remember that all arguments are passed by reference in Fortran as you showed in your example.

Let me know if you have any more questions.

CaKiwi
 
Hi, CaKiwi:

Thanks.

I use HP-UX. Now I found a way to link these object by using +ppu flag, which appends a underscore behind ABC, so that
f77 compiler can find ABC_().
In this case, should be ABC_(int *X), in
Fortran code, should be CALL ABC(X). In my previous post, order was wrong.

Binbin


 
G'day,

I have a program written in fortran 77 (test.f), in which I am trying to call a handful of C subroutines, written in addins.c. I am new to C, Fortran, compiling and programming in general, so I have a number of what may be simplistic questions...

Currently my fortran program just contains the command:
call subroutine(a,b,c)

How do I tell the program where this subroutine is?
Do I need external subroutine !$PRAGMA (addins.c),
or include addins.c,
or something similar?
And do I need to declare the subroutine as int subroutine or something prior to calling it?

In the C program, I currently just have the subroutines, and no main(). Do I need to include main()?

I think that the problem lies in compiling and then linking the two bits of code. How do I do this?
I am working on SunOS 5.8, using compilers cc (no gcc), and f77.
How can I compile the bits of code individually (if this is required) - as errors are given due to fortran not recgnising the C subroutines, and C not having a main().
Then how do I combine the two bits?

Thanks in advance,
CG
 
Here's a simple example

/* ------ C file addins.c ------ */

#define csub csub_
csub(int *i4)
{
*i4 = 1;
}

C ------ fortran file fc.f ------

program fc

integer*4 i4/0/

call csub(i4)

print *, 'i4 = ',i4

end

#Compile the C file with the -c option to create an object

cc -c addins.c

# Compile the fortran file and link with the C object

f77 fc.f addins.o

# run the executable

a.out

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Thanks, i've been banging my head against that particular brick wall for a while. Didn't realise before that you could compile without trying to make an executable (ie the cc -c option).
 
I have one more question:
If in the c funtion, I defined some macro and enum, and my c funtion call use these enum as argument, How can i call this function in fortran?
example:
in c:
typedef enum
{ X =0,
Y = 1
}FORMAT
...
void foo_(FORMAT myformat);
thanks a lot.
tan

 
You could probably pass it as an integer array, although if it is a global definition as your example implies, you would have to redefine it in fortran. The best way might be to write a function in C that could be called from fortran and would pass the appropriate enum to your foo_() function.

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top