-1

I am trying to link a header only library (which is in cpp) to a fortran code. I am using this example to test my library.

   $ cat cppfunction.C
   #include<cmath>
   #include<mylib/mylib.hpp>

   extern "C" 
   {
        void cppfunction_(float *a, float *b);
   }
   void cppfunction_(float *a, float *b)
   {
        *a=7.0;
        *b=9.0;
   }

  $ cat fprogram.f

   program fprogram

   real a,b
   a=1.0
   b=2.0

   print*,"Before fortran function is called"
   print*,'a=',a
   print*,'b=',b

   call cppfunction(a,b)
   print*,"After cpp function is called"
   print*,'a=',a
   print*,'b=',b

   stop
   end

For compiling I am using:

    $ gfortran -c fprogram.f
    $ g++ -c cppfunction.C
    $ gfortran -lc -o fprogram fprogram.o cppfunction.o

This runs fine if I remove my library header. But have this error when included:

    cppfunction.o: In function `__static_initialization_and_destruction_0(int, int)':
    cppfunction.C:(.text+0xa1): undefined reference to `std::ios_base::Init::Init()'
    cppfunction.C:(.text+0xb0): undefined reference to `std::ios_base::Init::~Init()'
    collect2: error: ld returned 1 exit status

Anything I might be doing wrong?

Ashish Magar
  • 65
  • 1
  • 9

1 Answers1

0

You're not linking the C++ standard library:

gfortran -lc -lstdc++ -o fprogram fprogram.o cppfunction.o
//           ^^^^^^^^
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055