本文介绍了将OSX Homebrew Gfortran与libc ++链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个包含大型C ++组件的项目,我能够在OSX(Apple LLVM版本6.1.0(clang-602.0.49)(基于LLVM 3.6.0svn)上)成功使用clang进行编译.提供我通过Homebrew安装gfortran的Fortran编译器.

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.

编译工作正常,但是我无法将编译的Fortran代码与之前编译的C ++代码链接:我收到以下错误:

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 [...]
[...]

这向我表明我在Fortran和C ++部分之间存在链接问题.

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

如何将Fortran零件与libc ++链接? Homebrew提供的gfortran是否有可能?解决此问题的最佳方法是什么?我应该尝试与clang ++链接吗?

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

推荐答案

您需要明确告诉gfortran链接clangs c ++库(它将默认为GNU c ++库).

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

例如,如果您有一个Fortran和C ++文件,则分别使用各自的编译器进行编译(请注意:gfortran-mp-5是macports提供的GNU Fortran 5.1)

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

您可以将生成的对象与gfortran链接在一起,如下所示:

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

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

-lc++标志告诉gfortran链接到libc++中,这将解析您未定义的符号.

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

这篇关于将OSX Homebrew Gfortran与libc ++链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:42