将 cpp header only 库链接到 fortran 时出现错误“对 `std::ios_base 的未定义引用”

Error " undefined reference to `std::ios_base" while linking cpp header only library to fortran

我正在尝试将 link 一个仅 header 的库(位于 cpp 中)转换为 Fortran 代码。我正在使用 this example 来测试我的图书馆。

   $ 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

编译我使用:

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

如果我删除我的库 header,它运行良好。但是包含的时候出现这个错误:

    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

我有什么地方做错了吗?

您没有链接 C++ 标准库:

gfortran -lc -lstdc++ -o fprogram fprogram.o cppfunction.o
//           ^^^^^^^^