473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

using fortran modules in c++

Hello all,

I am trying to interface c++ and fortran, the fortran code is already
there and I was successful
in calling the subroutines and functions, but was not able to send the
fortran module data into my C++ code and viceversa. I used gcc 3.3 and
g95 for c++ and fortran resp. Here is a little made up sample code so
that u could understand my problem.

$cat fort.f90
MODULE foo
real*8 a, b, c
INTEGER d
END MODULE foo

subroutine just ()
use foo
......
! some code which manipulates with the variables in the module just
! Now after manipulation i want them to be sent to my c++ code
......
end subroutine

$cat main.cpp

#include <iostream.h>
#include <math.h>

extern "C" { int just_ ( );}

// is there any structure for the module as well so that i could
//use its variables in c++

int main()
{

just_( );
// I actually wanted to access variables a,b of the module manipulate
them
// and pass it to the fortran like below
// can i do something like just_(&a,&b)
// do some manipulation in fortran and access the latest data in c++

return 0;

}

It wud be great if anyone could explain me how to do this,

Thanx in advance

Regards
Santosh

Jul 23 '05 #1
6 3663
Santosh wrote:
I am trying to interface c++ and fortran, the fortran code is already
there and I was successful
in calling the subroutines and functions, but was not able to send the
fortran module data into my C++ code and viceversa. I used gcc 3.3 and
g95 for c++ and fortran resp. Here is a little made up sample code so
that u could understand my problem.
[...]


Interlanguage connectivity is extremely compiler-specific and as such
is OT here. Please ask in a newsgroup for your compiler.

V
Jul 23 '05 #2
Santosh wrote:
Hello all,

I am trying to interface c++ and fortran, the fortran code is already
there and I was successful
in calling the subroutines and functions, but was not able to send the
fortran module data into my C++ code and viceversa. I used gcc 3.3 and
g95 for c++ and fortran resp. Here is a little made up sample code so
that u could understand my problem.

[snip]
Regards
Santosh


Try the gnu.g++.help newsgroup.
Jul 23 '05 #3
Santosh wrote:
Hello all,

I am trying to interface c++ and fortran, the fortran code is already
there and I was successful
in calling the subroutines and functions, but was not able to send the
fortran module data into my C++ code and viceversa. I used gcc 3.3 and
g95 for c++ and fortran resp. Here is a little made up sample code so
that u could understand my problem.

[snip]
It is a little bit off topic, but check out the example in
http://seehuhn.de/comp/linear#lapack-hard

You can also browse through the portland compiler manual. It will give
you interoperability in all directions. You will get the idea for any
compiler/platform from this platform.

Best,

Paul

Jul 23 '05 #4
Maybe using a program like "f2c" (fortran to C) would help?
<duck>
-Gernot
Jul 23 '05 #5
Gernot Frisch wrote:
Maybe using a program like "f2c" (fortran to C) would help?


I'm afraid it would not help, since f2c translates Fortran 77 to C, and
modules were introduced in Fortran 90.

Jul 23 '05 #6
Santosh wrote:
I am trying to interface C++ and Fortran,
the fortran code is already there
and I was successful in calling the subroutines and functions
but was not able to send
What do you mean by "send"?
the Fortran module data into my C++ code and viceversa.
I used gcc 3.3 and g95 for C++ and Fortran respexctively.
Here is a little made up sample code so
that you could understand my problem.

$cat fort.f90
MODULE foo
real*8 a, b, c
INTEGER d
END MODULE foo

subroutine just ()
use foo
! .....
! some code which manipulates with the variables in the module just
! Now after manipulation i want them to be sent to my c++ code
! .....
end subroutine

$cat main.cpp

#include <iostream.h>
#include <math.h>

extern "C" { void just_(void); }

// Is there any structure for the module as well
// so that I could use its variables in C++

int main(int argc, char* argv[]) {
just_();
// I actually wanted to access variables a, b of the module,
// manipulate them and pass it to the Fortran like below.
// Can I do something like just_(&a, &b),
// do some manipulation in Fortran and access the latest data in C++

return 0;
} cat fort.f90 MODULE foo
real*8 a, b, c
INTEGER d
END MODULE foo
g95 -c fort.f90
nm fort.o 0000000000000000 B foo_MP_a
0000000000000018 B foo_MP_b
0000000000000008 B foo_MP_c
0000000000000010 B foo_MP_d

Unfortunately, there is no standard for mangling
module variable names to generate the symbols left behind
in the object file for the link editor.
Some compilers generate symbols that aren't even
valid C identifiers so you can't reference them
from a C (or C++) program directly.
cat just.f90 subroutine just()
use foo
a = 13.0
b = 42.0
end subroutine just

subroutine get(x, y)
use foo
real, intent(out)::x, y
x = a
y = b
end subroutine get
g95 -c just.f90
cat main.cc #include <iostream>

extern "C" {
void just_(void);
void get_(float&, float&);
}

int main(int argc, char* argv[]) {
just_();
float a, b;
get_(a, b);
std::cout << "a = " << a << '\t'
<< "b = " << b << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc just.o fort.o
./main

a = 13 b = 42
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Markus Faust | last post by:
Hi, I'm trying to link Fortran files generated with “Compaq Visual Fortran Optimizing Compiler Version 6.6 (Update B)” under “Enthought Edition build 1028, Python 2.3 (#46, Aug 11 2003,...
5
by: Carl | last post by:
I have experimented with f2c and swig-generated wrappers to create python modules from Fortran files. I think I'm missing something when I'm building the Python module, because when I import the...
7
by: Anonymous | last post by:
I have a mixed-language pgm (Fortran and C++) which needs to pass a few hundred values from C++ to Fortran. One way to do this is to have a Fortran module and a C++ structure (or class) with an...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
26
by: sam | last post by:
Hi, Can anyone help me find a software that can convert a code in 'C' to 'Fortran77/90' automatically? Thanks in advance. Sam.
11
by: Cottonwood | last post by:
I want to call a GNU-Fortran-library from a GNU-C++ source. Is that possible at all? If yes, what is to be done to link the program? I get an error message that says that the module MAIN_ cannot...
21
by: Cottonwood | last post by:
I want to call a C module from a Fortran program. Whatever I tried - the linker could not find the C module. I know about the leading underscore and switched even that off. I abstracted everything...
3
by: sam | last post by:
hello all, i am currently in the process of planning a piece of software to model polymerisation kinetics, and intend to use python for all the high-level stuff. the number-crunching is...
13
by: Mangabasi | last post by:
Howdy, I have been trying to call the following Fortran function from Python (using Windows XP, Compaq Fortran and Python 2.4). I tried F2Py, Pyfort and calldll with no success. I think I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.