“__gfortran_pow_c8_i4” error when linking .o files from g++ and gfortran using g++

后端 未结 1 1787
盖世英雄少女心
盖世英雄少女心 2020-12-11 12:03

I am trying to link a .o file generated using g++ and another .o file generated using gfortran.

g++ -c mycppcode.cpp

produces the file

相关标签:
1条回答
  • 2020-12-11 12:42

    The following assumes you are using the GNU compiler tools. Things may be slightly different if you are using other compilers.

    You can use either compiler to link the two together, but you need to provide the appropriate libraries.

    Typically, you can use either

    gfortran fortobj.o cppobj.o -lstdc++
    

    or

    g++ fortobj.o cppobj.o -lgfortran
    

    This assumes that you are using a setup where both compilers know about each other's libraries (like if you installed through a linux repository).


    In the case of the OP the C compilers came from XCode and gfortran is from homebrew. In that case, gfortran knows about the g++ libraries (since they were used to compile the compiler), but g++ doesn't know about the gfortran libraries. This is why using gfortran to link worked as advertised above. However, to link with g++ you need to add the path to libgfortran.* when you call the linker using the -L flag, like

    g++ fortobj.o cppobj.o -L/path/to/fortran/libs -lgfortran
    

    If for some reason your gfortran compiler is unaware of your g++ libs, you would do

    gfortran fortobj.o cppobj.o -L/path/to/c++/libs -lstdc++
    

    Note that there shouldn't be any difference in the final executable. I'm no compiler expert, but my understanding is that using the compiler to link your objects together is a convenience for calling the linker (ld on UNIX-like OS's) with the appropriate libraries associated with the language you are using. Therefore, using one compiler or the other to link shouldn't matter, as long as the right libraries are included.

    0 讨论(0)
提交回复
热议问题