Link OSX Homebrew Gfortran against libc++

穿精又带淫゛_ 提交于 2019-12-07 05:59:48

问题


I have a project with a large C++ component that I was able to successfully compile with clang on OSX (Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn). Since OSX does not provide a Fortran compiler I installed gfortran via Homebrew.

Compilation works fine, however I can not link the compiled Fortran code against the C++ code compiled earlier: I get the following error:

$ make fortran
Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::compare(char const*) const", referenced from:
      DataFieldInfo::FromJSON(JSONNode const&) in [...]
  "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
      std::__1::vector<char, std::__1::allocator<char> >::allocate(unsigned long) in [...]
      void std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::__push_back_slow_path<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in [...]
      void std::__1::vector<JSONNode, std::__1::allocator<JSONNode> >::__push_back_slow_path<JSONNode const>(JSONNode const&) in [...]
[...]

Which indicates to me that I'm having a linking problem between the Fortran and the C++ part.

How do I link Fortran part with libc++? Is this possible with gfortran provided by Homebrew? What would be the best course of action to solve this issue? Should I try linking with clang++?


回答1:


You need to explictly tell gfortran to link against clangs c++ library (it will default to the GNU c++ library).

For example, if you have a Fortran and C++ file, each compiled with their respective compilers (note: gfortran-mp-5 is GNU Fortran 5.1 provided by macports)

gfortran-mp-5 -c gfortest.f90
clang++ -c clangtest.cc

You can link the resulting objects together with gfortran as follows:

gfortran-mp-5 -lc++ -o test-f gfortest.o clangtest.o

The -lc++ flag tells gfortran to link in libc++, which will resolve your undefined symbols.



来源:https://stackoverflow.com/questions/30174180/link-osx-homebrew-gfortran-against-libc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!