Results 1 to 3 of 3
Hello,
I have a difficulty in Mixed language programming. There are many global variables in a fortran module, which have to be accessed from C++ main program. Although the compiler ...
- 09-16-2009 #1Just Joined!
- Join Date
- Sep 2009
- Posts
- 4
C++ Fortran integration in Linux
Hello,
I have a difficulty in Mixed language programming. There are many global variables in a fortran module, which have to be accessed from C++ main program. Although the compiler is giving no errors with the following simple structure, but, linker gives errors with - 'undefined reference to the common variable in C++ object'.
fortran module:
MODULE EXAMP
SAVE
REAL, X(4), Y(4), Z(4)
REAL, PARAMETER :: Pi =3.14159
END MODULE EXAMP
C++:
#include<iostream>
extern "C" float EXAMP_mp_X[4];
extern "C" float EXAMP_mp_Y[4];
extern "C" float EXAMP_mp_Z[4];
extern "C" float EXAMP_mp_Pi;
using namespace std;
int main()
{
.....
....
}
I am using Linux...g++ and gfortran compiler in the following way
gfortran -c mod.f
g++ -c testc.cpp
g++ -o test mod.o testc.o -llapack
Could anyone please share their experience in handling the similar problem. Thanking you,
- 09-16-2009 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
I looked through the man pages for gfortran and g++, and there are a lot of references to symbols, I didn't see any that would help solve this problem.
I used nm to list the symbols from both compiled sources. Here are the Fortran and cc that I used:
and the main:Code:% cat mymod.f90 MODULE EXAMP SAVE REAL X(4), Y(4), Z(4) REAL, PARAMETER :: Pi =3.14159 real mypi data mypi / pi / END MODULE EXAMP
I compiled, linked and executed with this:Code:% cat main.cc #include<iostream> extern "C" float EXAMP_mp_X[4]; extern "C" float __examp_MOD_x[4]; extern "C" float EXAMP_mp_Y[4]; extern "C" float EXAMP_mp_Z[4]; // extern "C" float EXAMP_mp_Pi; extern "C" float __examp_MOD_mypi; using namespace std; int main() { cout << " Hello, world from c++\n"; // cout << EXAMP_mp_X[0]; cout << " x[0] is " << __examp_MOD_x[0] << endl; cout << " mypi is " << __examp_MOD_mypi << endl; cout << " Goodbye.\n"; }
Which produced:Code:% cat doit #!/bin/bash # Abort on any error. set -o errexit # Compile. g++ -c main.cc gfortran -c mymod.f90 # Link. g++ *.o # Look at symbols. echo nm *.o # Execute. echo ./a.out
This shows that the symbols in the original sources did not match. When they are changed as in the new version, the code runs.Code:% ./doit main.o: 00000000000000d4 t _GLOBAL__I_main 0000000000000097 t _Z41__static_initialization_and_destruction_0ii U _ZNSolsEPFRSoS_E U _ZNSolsEf U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev U _ZSt4cout U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_ 0000000000000000 b _ZStL8__ioinit U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc U __cxa_atexit U __dso_handle U __examp_MOD_mypi U __examp_MOD_x U __gxx_personality_v0 0000000000000000 T main mymod.o: 0000000000000000 D __examp_MOD_mypi 0000000000000000 B __examp_MOD_x 0000000000000010 B __examp_MOD_y 0000000000000020 B __examp_MOD_z Hello, world from c++ x[0] is 0 mypi is 3.14159 Goodbye.
Someone may be able to suggest a better solution, but this seems to work in the meantime.
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 09-17-2009 #3Just Joined!
- Join Date
- Sep 2009
- Posts
- 4
Thank you very much Mr. drl. Your approach seems to work for my problem.
Best Regards,


Reply With Quote