LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 02-25-2018, 05:52 AM   #1
sailschooner
LQ Newbie
 
Registered: Jun 2015
Posts: 28

Rep: Reputation: Disabled
calling c. subroutine from fortran


I'm trying to use the following no problem gcc-compilable proram

void sub_(i,j,k)
int *i, *j, *k;
{
*k = *i + *j;
return;
}

as a subroutine in a Lahey lf95 fortran program:

PROGRAM MAIN
integer :: i,j,k
i = 12
j = 43
k = 0
print *, 'Before: i,j,k=',i,j,k
call sub(i,j,k)
print *, 'After: i,j,k=',i,j,k
stop
end

Obviously the fortran part needs compiling, but do I use sub.c or sub.o (or something else). What would be the command to run them? Also, am I likely to need USE and/or INCLUDE, I intend to build them into a line-plotting program later. Many thanks

Adrian
 
Old 02-25-2018, 10:24 AM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 784

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
I don't know anything about Lahey fortran but here's how to compile with gfortran:
Code:
$ gfortran -c fort_file.f95
$ gcc -c c_file.c
$ gfortran -o app fort_file.o c_file.o
$ ./app
 Before: i,j,k=          12          43           0
 After: i,j,k=          12          43          55
HTH
 
1 members found this post helpful.
Old 02-26-2018, 12:02 PM   #3
masterclassic
Member
 
Registered: Jun 2007
Distribution: Knoppix
Posts: 154

Rep: Reputation: 36
Hello.
I didn't use Lahey fortran since early 90s, however I remember well that an issue in multiple language programming is related to how the procedures/subroutines communicate their arguments: by reference or by value. You have to use the same on both parts.
 
1 members found this post helpful.
Old 02-26-2018, 02:22 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,664
Blog Entries: 6

Rep: Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

Quote:
Originally Posted by sailschooner View Post
Obviously the fortran part needs compiling, but do I use sub.c or sub.o (or something else). What would be the command to run them?
norobro has provided the answer - build the object files separately from fortran and C source files, then build the application from the object files.
 
1 members found this post helpful.
Old 02-27-2018, 07:58 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165Reputation: 3165
Quote:
Originally Posted by masterclassic View Post
Hello.
I didn't use Lahey fortran since early 90s, however I remember well that an issue in multiple language programming is related to how the procedures/subroutines communicate their arguments: by reference or by value. You have to use the same on both parts.
A traditional problem with FORTRAN is both "pass by value or reference" and the in-memory layout of arrays. Some FORTRANs use a "column-major format" where most languages use "row-major."

Naturally, you must also get the "calling sequence" right – exactly how parameters are passed to-and-from the subroutine. But that's true of any language implementation.

The documentation of any compiler should contain a substantial section discussing exactly how to call to or from its subroutines, how to build and use binary libraries of various kinds (dynamic or static), and so on. Probably giving examples.

Last edited by sundialsvcs; 02-27-2018 at 08:00 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Fortran LAPACK subroutine DSYEV: order of eigenvalues. C.L. Programming 1 11-09-2011 10:52 AM
How to call fortran subroutine from IDL seflyer Programming 1 10-16-2010 01:05 PM
how calling subroutine in Perl shifter Programming 1 08-19-2010 11:12 AM
Calling Fortran 95 code in C srunni Programming 2 04-15-2009 08:15 AM
calling C from fortran RudraB Programming 0 07-23-2008 12:52 AM

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

All times are GMT -5. The time now is 08:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration