Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

linking C++ with fortran90

doctoromega1234
Beginner
3,704 Views
Hello!

I have two object files. One was created from a .f90 through ifort version 9.0, the other is a C++ file, compiled with g++ version 3.4.6. I'm running on i686 AMD 64 Processor 3200+ GNU/Linux. The code is as follows :
------F90 --------------
SUBROUTINE FORMAIN

IMPLICIT NONE

INTEGER :: x

CALL TEST(x)

WRITE (*,*) x
RETURN
END
----------------------

------C++-----

#include

using namespace std;

extern "C" {
void test_(long* a){
*a=1;
}
}

extern "C" int formain_();

int main() {

return formain_();
}

------------------

Both compile fine with their respective compilers. My code
should be correct, I just want to display the value of x from the
fortran code
.

using g++ as the linker: g++ -o test fortran.o c++.o

Produces error:

fortran.o: In function 'mymain_':
fortran.f90: (.text+0x3a): undefined reference to
'for_write_seq_lis'
collect2: ld returned 1 exit status

I think my problem is a result of missing libary files.
How can I point g++ to read these libary files?
Or alternatively, how can I point ifort to read the appropriate c++ files
from g++?

Please please help, it's drving me nuts lol.

Thanks,

-Jason.





0 Kudos
9 Replies
Steven_L_Intel1
Employee
3,704 Views
Best choice - use ifort as the linker. It should find the g++ files automatically. You may need -nofor_main to tell it not to look for a Fortran main program
Second choice - read the section of the Building Applications manual that discusses linking and selection of libraries
0 Kudos
doctoromega1234
Beginner
3,704 Views
I compiled it with the -nofor_main flag like you suggested (thanks) but still it's being difficult!
I don't think it's finding the libaries like it should. I get the following:

cplus.o: In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
shouldwork.cc:(.text+0xd): undefined reference to `std::string::size() const'
shouldwork.cc:(.text+0x60): undefined reference to `std::string::operator[](unsigned int) const'
shouldwork.cc:(.text+0x9d): undefined reference to `std::string::operator[](unsigned int) const'
shouldwork.cc:(.text+0xc8): undefined reference to `std::string::operator[](unsigned int) const'
cplus.o: In function `__static_initialization_and_destruction_0(int, int)':
shouldwork.cc:(.text+0x154): undefined reference to `std::ios_base::Init::Init()'
cplus.o: In function `__tcf_0':
shouldwork.cc:(.text+0x183): undefined reference to `std::ios_base::Init::~Init()'
cplus.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'

This is a problem with the libaries isn't it? If that's the case how do I go
about fixing it, I can't quite seem to get it lol. I hope it's not my code.

Thanks,

-Jason.
0 Kudos
TimP
Honored Contributor III
3,704 Views
When you use ifort (or gnu Fortran) to link with C++, you must append -lstdc++ in order to link against libstdc++.
In looking at the source you posted the first time around, if you are using 64-bit compilation, there may be a type mismatch. Fortran default integer would match C++ int type. If your C++ compiler treats long as a 64-bit type, you would have to use the KIND facility or the -i8 compile flag to get 64-bit integer in ifort.
0 Kudos
doctoromega1234
Beginner
3,704 Views
Thanks for the help,

Using what you said I did the following:

ifort -o test fortran.o cplus.o -L /usr/lib/gcc/i586-pc-linux/3.4.6/ -lstdc++ -nofor_main

and it compiled! However! hah, nothing happens. It wont output to the screen, and I even included a cout<<*a; following the statement *a=1; Anyone know what's going on?

Thanks,

-Jason.

(I did the compiler command correctly right? It worked, but the code should work)

0 Kudos
Steven_L_Intel1
Employee
3,704 Views
Well, it seems as if it should work, but I'm not a C++ expert. Perhaps you can print out the value of the variable on return to the Fortran code to see if it actually got set?
0 Kudos
Lorri_M_Intel
Employee
3,704 Views

If Imay be allowed a slight tangent here ...

Multiple versions ago, using the Fortran driver did automatically link against C++ libraries, but that caused problems with a variety of Linux distros.

So, by the default, the Fortran driver no longer automatically links against C++ libraries.

However -- it will do it for you if you use -cxxlib, as:

ifort -cxxlib -nofor_main file1.o file2.o ... etc

That way you don't need to remember the names of any of the libraries.

Back to your regularly scheduled topic now.

- Lorri

0 Kudos
doctoromega1234
Beginner
3,704 Views
Hey guys, just to let you know the reason why It wont display the write statement is because it is inside the subroutine. Fortran doesn't like to have write statements in there lol. That's about it I can compile things great now, thanks guys.


0 Kudos
Steven_L_Intel1
Employee
3,704 Views
Fortran has no problems with WRITE statements in subroutines. What makes you think otherwise?
0 Kudos
shakeham
Beginner
3,704 Views
for example I have fortstub.f90 and myC.cpp. C++ calls fortran. I could compile the code with

ifort fortstub.f90 -c -g
icc myC.cpp -g -c
icc myC.o fortstub.o -lifcore -o test

but we need to make sure the compiler libraries are in LD_LIBRARY_PATH. It should be there when ifort was installed. However if for some reason it is not there, the command can be augmented as
icc myC.o fortstub.o -L ~/intel/composer_xe_2011_sp1.10.319/compiler/lib/intel64/ -lifcore -o test

where that library is would depend on the installation and version
0 Kudos
Reply