【问题标题】:Installing C++ NetCDF4 library on MacOS via MacPorts fails通过 MacPorts 在 MacOS 上安装 C++ NetCDF4 库失败
【发布时间】:2021-09-05 00:18:40
【问题描述】:

我已经通过 Macports 安装了 netcdf-cxx4 并从这里下载了一个 C++ NetCDF4 示例: https://www.unidata.ucar.edu/software/netcdf/examples/programs/SimpleXyWr.cpp

我不太熟悉设置链接选项,我尝试使用以下命令编译/链接程序:

g++ SimpleXyWr.cpp -o SimpleXyWr.out -I/opt/local/include -L/opt/local/lib -lnetcdf -lnetcdf_c++4

但是,这会导致以下错误:

Undefined symbols for architecture x86_64:
  "__ZN6netCDF6NcFileC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS0_8FileModeE", referenced from:
      _main in cco9p4yv.o
  "__ZNK6netCDF7NcGroup6addDimERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm", referenced from:
      _main in cco9p4yv.o
  "__ZNK6netCDF7NcGroup6addVarERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_6NcTypeERKSt6vectorINS_5NcDimESaISD_EE", referenced from:
      _main in cco9p4yv.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我完全不知道如何解决这个问题。

更新:我部分解决了问题:我意识到 MacPorts 使用 clang 来编译端口。因此,我尝试使用以下命令安装 netcdf-cxx4:

sudo port -s -v install netcdf-cxx4 configure.compiler=macports-gcc-10

安装结束:

netcdf-cxx4 is using libstdc++ (this installation is configured to use libc++)
--->  Found 1 broken port, determining rebuild order
You can always run 'port rev-upgrade' again to fix errors.
The following ports will be rebuilt: netcdf-cxx4 @4.3.0
Continue? [Y/n]:

如果我输入 Y,netcdf-cxx4 会重新安装(但再次使用 clang - 这对我没有帮助)。如果我中止,我可以使用 NetCDF4 库(一切似乎都正常)。但是,为了解决上述问题,我尝试使用以下命令安装 netcdf-cxx4:

sudo port -s -v install netcdf-cxx4 configure.compiler=macports-gcc-10 configure.cxx_stdlib=macports-libstdc++

但这以错误消息结束:

checking whether the C compiler works... no
configure: error: in `/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_devel_gettext/gettext/work/gettext-0.19.8.1/gettext-runtime':
configure: error: C compiler cannot create executables
See `config.log' for more details
configure: error: ./configure failed for gettext-runtime
Command failed:  cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_devel_gettext/gettext/work/gettext-0.19.8.1" && ./configure --prefix=/opt/local ac_cv_prog_AWK=/usr/bin/awk ac_cv_path_GREP=/usr/bin/grep ac_cv_path_SED=/usr/bin/sed --disable-csharp --disable-java --disable-native-java --disable-openmp --without-emacs --with-included-gettext --with-included-glib --with-included-libcroco --with-included-libunistring --with-included-libxml --without-cvs --without-git --without-xz 
Exit code: 77
Error: Failed to configure gettext, consult /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_devel_gettext/gettext/work/gettext-0.19.8.1/config.log
Error: Failed to configure gettext: configure failure: command execution failed
Error: See /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_devel_gettext/gettext/main.log for details.
Error: Unable to execute port: upgrade netcdf failed

我能做些什么来解决这个问题?感谢您的任何意见!

【问题讨论】:

  • 你能grep那些缺少符号名称的库吗?
  • 感谢您的建议。看来我缩小了问题的原因(请参阅上面的更新信息)。

标签: c++ linker-errors macports netcdf4


【解决方案1】:

您正确推断出问题在于 netcdf-cxx4 是 MacPorts 使用 clang++ 编译的,而您尝试使用 g++ 编译程序。 clang++ 使用 libc++ 标准库,而 g++ 使用 libstdc++ 标准库;它们彼此不兼容。 C++ 程序及其使用的所有库必须使用相同的 C++ 库。

您的错误在于试图通过在命令行上覆盖 configure.compiler 来猜测 MacPorts 对编译器的选择。 MacPorts 不是为您设计的。 (在命令行上覆盖变量的能力旨在帮助 MacPorts 开发人员调试问题。)相反,您应该做以下两件事之一:

  1. 让 MacPorts 使用 clang++ 编译 netcdf-cxx4 并使用 clang++ 编译您的程序。这是推荐的选项,因为 clang 是 macOS 上的首选编译器。或者:
  2. 运行port variants netcdf-cxx4 并发现netcdf-cxx4 端口的作者已经预料到您可能希望使用不同的编译器对其进行编译,并提供了使您能够这样做的变体。例如,您可以运行 sudo port install netcdf-cxx4 +gcc10 以使用 MacPorts gcc 10 对其进行编译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-20
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多