- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 :
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.
Link Copied
9 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Second choice - read the section of the Building Applications manual that discusses linking and selection of libraries
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Fortran has no problems with WRITE statements in subroutines. What makes you think otherwise?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page