LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2005, 02:08 AM   #1
bigapple
LQ Newbie
 
Registered: Oct 2004
Location: Hangzhou.China
Distribution: Suse 9.3 | redhat 9.0 | Solaris 10
Posts: 29

Rep: Reputation: 15
how to call C function in FORTRAN program


hi ,all
I want to write a FORTRAN program,and in the program I call a C function.the code is
test1.for
Code:
C--CALL A C FUNC
PROGRAM caller
integer*2 temp
temp = test()
print *,temp
END
test2.c
Code:
int test(void){
    return 100;
}
I use GCC and XLF to compile the object file: test1.o test2.o And then use XLF link them together.but when I run the program,it print 0 on my screen.
I have no experience in FORTRAN programming,Anyone who konw this language please help ,thanks for your time!!
 
Old 08-22-2005, 09:32 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
1. The problem might be incompatible binaries.

I don't know what "xlf" is, but you might consider using gfortran (i.e. GNU Fortran should be most compatible with GNU C).

2. The problem might be incompatible argument types

"integer*2" is a 16-bit integer. The default GNU C "int" for x86 platforms is 32-bit integers.

Try "integer*4" instead.

'Hope that helps .. PSM

Last edited by paulsm4; 08-22-2005 at 09:34 AM.
 
Old 08-22-2005, 12:33 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Compilers generally produce compatible object-files, so applications can be written in more than one language simultaneously. The most important thing you need to watch for is the calling convention.

This refers to the exact mechanism by which a CALL statement is carried out:
  • Exactly how the return-address and parameters are provided to the called function.
  • The order (left-to-right or right-to-left) in which the parameters are passed.
  • Who's responsible for cleaning-up the stack.
Once you have that all sorted out, you also need to observe whether parameters are passed by value or by reference in each CALL. When a parameter is passed "by value," its actual value is placed on the stack; "by reference," the address of the value is placed on the stack. In addition, as previously mentioned, the data-types must be compatible... such as the size of an INTEGER.

Yet another "thorn in the side" of mixed-language programs is the issue of runtime libraries. Most programming-languages implement many of their features by creating calls to a rather large library of routines, such as GNU's glibc, which must be available. The program initialization and termination sequences generally include implied-calls to initialize and clean-up those libraries. Special options and/or command-line switches may be required when compiling programs for use in a mixed-language environment.

The documentation for your particular compiler should be the authoritative source and should provide examples.
 
Old 08-22-2005, 01:50 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Please consider the original advice...

C'mon, sundialsvcs. Please give bigapple a break.

I think we're both saying pretty much the same thing, and I think we both want to make it as simple and easy as possible for bigapple to find the right solution. My two points were:

1. runtime library compatibility is a big issue
a) Ideally, bigapple can find a GNU FORTRAN compiler to use with his GNU C. That should minimize
risk of library incompatibilities.

b) Alternatively, bigapple can find an IBM xlc compiler to go with his xlf compiler. Either a) or b) should work fine.

2. The data types passed between FORTRAN an C need to match.
a) In this case, 'C' is undoubtedly expecting a 32 bit int, and bigapple is instead passing a 16-bit "integer*2"
b) I still suggest that he change his FORTRAN code to integer*4

3. Calling order and linkage (C vs Pascal stack order, upper vs lower vs mixed case names, default by value
or by reference, etc etc) are all potentially significant. But these all tend to be COMPILER SPECIFIC and,
I thought the most important suggestion was to get him on a SINGLE compiler family for BOTH LANGUAGES.

In my experience, things "just work" when both compilers are in the same family (in other words, most
compiler families share the same back ends and, consequently, most object binaries are completely
interoperable).

4. The following links might be helpful:

GF77/G95 (GNU FORTRAN 77 and FORTRAN 95):
http://gcc.gnu.org/onlinedocs/gfortran/

IBM white paper on linking IBM FORTRAN programs with IBM C
http://publib.boulder.ibm.com/doc_li.../FORTRANnC.htm

IBM/AIX main documentation page:
http://publib.boulder.ibm.com/cgi-bin/ds_form
 
Old 08-23-2005, 11:53 PM   #5
bigapple
LQ Newbie
 
Registered: Oct 2004
Location: Hangzhou.China
Distribution: Suse 9.3 | redhat 9.0 | Solaris 10
Posts: 29

Original Poster
Rep: Reputation: 15
Thanks for all of your help ,I have got the answer.
xlf is a default FORTRAN compiler on AIX(Ibm's UNIX operation system).and i use xlC_r to compile my C codes on AIX.

in my code,I forget this statement
Code:
integer*2 test
Because the function need to be declared in FORTRAN as it in C language.

I will continue learing FORTRAN,and I have found the languge also very powerful, It has pointers,structure,,,eg.

At last,,Thanks for all your help ,,aha

Last edited by bigapple; 08-23-2005 at 11:58 PM.
 
Old 08-24-2005, 12:30 AM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Please do me a big favor: define all your 'C' variables that accept "integer*2" from FORTRAN as "short" (or better, int16_t), and define all your FORTRAN variables that accept 'int' from 'C' as "integer*4"?

Please?

Your life should be much happier if you do so ;-)

PS:
FORTRAN has a directive, "IMPLICIT NONE".

It would have saved you in this problem. Please *always* consider using "implicit none" in all new Fortran modules that you write.

IMHO...

Last edited by paulsm4; 08-24-2005 at 09:36 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gui for a fortran program kooshball Programming 2 06-06-2005 08:37 AM
How to call another function from a function? geminigal Programming 4 04-21-2005 10:41 PM
No Matching Function for Call ! vipinsharma Programming 2 07-05-2004 01:58 PM
why doesn't my fortran program compile?? himan Programming 1 04-11-2004 10:43 PM
how to call function?? harpal Programming 3 04-29-2003 05:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration