2

I should couple in linux one c++ code with old fortran code, where fortan is the main code. Im not expert in this area and I try to start with simple test, but still I cannot compile it. Maybe I'm stupid, but I cannot find a working example anywhere. I managed to compile fortran and c, when the linking can be done by ifort (need to use intel compiler later with the actual fortran code). But If I've understood right, with c++, the linking must be done by c++ compiler (g++).

So what do I do wrong here:

My FORTRAN test code "ftest.f":


PROGRAM MAIN

  IMPLICIT NONE
  INTEGER I
  write(*,*) "hello fortran1"
  CALL ctest()
  write(*,*) "hello fortran2"

END PROGRAM

And C++ code "ctest.cpp"


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>

extern "C" void ctest_();

void ctest_(){
   int i;
 //   std::cout << "hello c \n";
   printf("hello c\n");
}

I try to compile with the following:

ifort -c ftest.f
g++ -c ctest.cpp
g++ -ldl -lm -limf -L -l -lifcore ctest.o ftest.o

And I get an error:

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

So what should I do to success with linking this program?

zvisofer
  • 1,346
  • 18
  • 41
  • Can't you use GCC to compile the fortran code? AFAIR the GCC compiler suite supports fortran. – πάντα ῥεῖ Feb 06 '14 at 10:40
  • Well the problem was that the actual fortran program, which i havet o combine with c++, is really old and big code with compiler dependend programming. Compiling it with gcc give sslightly wrong results. And correcting this, would be a huge work. – user3279036 Feb 06 '14 at 11:19

2 Answers2

4

Your main (entry) is in Fortran part, so one way to solve it is to to use ifort linker instead of g++ (that would also link ifcore automatically)

ifort ctest.o ftest.o ... -lstdc++
Peter Petrik
  • 9,701
  • 5
  • 41
  • 65
  • Hi! Thanks, but as I said, at least I was reading that with c++, you have to use c++ linker. If i use ifort, i get errors: – user3279036 Feb 06 '14 at 11:00
  • 1
    While the `ifort` run-time will be linked in automatically, that for `g++` then won't be. Something like `-lstdc++` in the "..." would be required. – francescalus Feb 06 '14 at 11:00
  • Hmm, the edit doesn't even work. SO with ifrot linker i get errors: ctest.o: In function `__static_initialization_and_destruction_0(int, int)': ctest.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()' ctest.o: In function `__tcf_0': ctest.cpp:(.text+0x66): undefined reference to `std::ios_base::Init::~Init()' ctest.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0' – user3279036 Feb 06 '14 at 11:01
  • THIS is WEIRD, I earlier tried linking with ifort and used also this -lstdc++ but it didn't work. Then I read this comment elsewhere that you have to use c++ linker so I didn't focus more on those errors in my previous comment. But now it WORKS!!! Maybe then something else was wrong with my source or something, but anyway now it works. THANK YOU very much! – user3279036 Feb 06 '14 at 11:05
  • So, can you use ifort for linking (so you are OK with the answer) or you have to use g++? – Peter Petrik Feb 06 '14 at 11:10
  • I am now ok with the answear. I don't know what went wrong when i earlier tried to link with ifort -lstdc++ but now it works and I don't even want to know what went wrong earlier ;) But that caused me this desperacy, when I thought that the working solution is wrong and then i tried to find another solutions. – user3279036 Feb 06 '14 at 11:17
1

So looks like I truested too much on one page telling me that I have to use c++ compiler for linking. Earlier just always something else was wrong when trying to link by ifort.

So using ifort with -lstdc++ is really enough with the current version of my test code. Earlier just something else was wrong.

Thank you very much once again, I wish you all the best for your own projects!