Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

c++ - "__gfortran_pow_c8_i4" error when linking .o files from g++ and gfortran using g++

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 mycppcode.o and the command

gfortran -c myfortrancode.f

produces the file myfortrancode.o

When I link these two files to get an output file

g++ -O mycppcode.o myfortrancode.o

I get the following error

Undefined symbols for architecture x86_64:
  "__gfortran_pow_c8_i4", referenced from:

Could some one help me with this? Should I use another compiler? Also, I would like to know what functions or subroutines call "__gfortran_pow_c8_i4", so that I can try to avoid these functions or subroutines in fortran in future.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...