Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link OSX Homebrew Gfortran against libc++

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++?

like image 860
Pascal Avatar asked Sep 28 '22 23:09

Pascal


1 Answers

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.

like image 58
casey Avatar answered Oct 05 '22 08:10

casey