【问题标题】:Compiling Fortran netCDF programs on Ubuntu在 Ubuntu 上编译 Fortran netCDF 程序
【发布时间】:2012-12-06 03:58:28
【问题描述】:

好的,这里是新手问题。

我正在尝试编译simple_xy_wr.f90 -- 一个 netCDF 示例程序 -- 在 Ubuntu 上使用 gfortran,我一定是在做一些非常愚蠢的事情;我没有太多编译 Fortran 的经验。

首先,我已经安装了libnetcdf-dev 包,其中包含类似

/usr/lib/libnetcdf.a
/usr/lib/libnetcdff.a
/usr/include/netcdf.mod

所以,我尝试使用(各种命令等)编译代码

f95 -o xy -I/usr/include/ -L/usr/lib/ -lnetcdff -lnetcdf simple_xy_wr.f90

我得到以下输出

/tmp/ccE6g7sr.o: In function `check.1847':
simple_xy_wr.f90:(.text+0x72): undefined reference to `__netcdf_MOD_nf90_strerror'
/tmp/ccE6g7sr.o: In function `MAIN__':
simple_xy_wr.f90:(.text+0x284): undefined reference to `__netcdf_MOD_nf90_create'
simple_xy_wr.f90:(.text+0x2b6): undefined reference to `__netcdf_MOD_nf90_def_dim'
simple_xy_wr.f90:(.text+0x2e8): undefined reference to `__netcdf_MOD_nf90_def_dim'
simple_xy_wr.f90:(.text+0x432): undefined reference to `__netcdf_MOD_nf90_def_var_manydims'
simple_xy_wr.f90:(.text+0x468): undefined reference to `__netcdf_MOD_nf90_enddef'
simple_xy_wr.f90:(.text+0x4aa): undefined reference to `__netcdf_MOD_nf90_put_var_2d_fourbyteint'
simple_xy_wr.f90:(.text+0x4cb): undefined reference to `__netcdf_MOD_nf90_close'
collect2: error: ld returned 1 exit status

我认为我包含了正确的库。例如。似乎 __netcdf_MOD_nf90_strerror 应该在那里:

$ nm /usr/lib/libnetcdff.a | grep __netcdf_MOD_nf90_strerror
000000000004a100 T __netcdf_MOD_nf90_strerror

我做错了什么?

(FWIW,我看过的一些相关参考资料如下。

  1. undefined reference using netcdf library

  2. Compiling problems with gfortran and NETCDF

  3. Compiling and Running Fortran Programs - a basic guide

)

【问题讨论】:

  • 你使用哪个编译器?
  • 如前所述,我使用的是 gfortran。
  • 试试这个:gfortran -o xy -I/usr/include/ -L/usr/lib/ -lnetcdf simple_xy_wr.f90 /usr/lib/libnetcdf.a.
  • 好的,我很困惑你使用 f95 调用它(可能是符号链接,在我的机器上调用不同的编译器)。
  • 尝试将两个库放在源文件之后。链接器按照它在列出的档案中搜索未解析的外部符号的顺序很挑剔。 (尽管您的命令行在 Ubuntu 8.08 上运行良好)

标签: fortran netcdf


【解决方案1】:

在 Unix 系统上,链接器命令行上的目标文件和档案的顺序非常重要,因为默认的链接器行为是仅在 遵循的档案中搜索符号定义找到未解析引用的目标文件或存档称为单通道链接。

这意味着如果您的代码引用__netcdf_MOD_nf90_strerror,则包含此符号定义的存档 (libnetcdff.a) 必须出现在您程序的目标文件列表之后。 libnetcdff.a 本身引用了来自 C 库 libnetcdf.a 的符号,因此它必须在 libnetcdff.a 之后链接。所以正确的链接顺序是:

/tmp/ccE6g7sr.o libnetcdff.a libnetcdf.a

其中/tmp/ccE6g7sr.o 是汇编器从已编译的源文件生成的临时目标文件。编译代码的正确命令行是:

f95 -o xy -I/usr/include/ simple_xy_wr.f90 -lnetcdff -lnetcdf

在这种情况下,不直接调用链接器,而是由编译器调用。 GCC 编译器将所有与链接相关的东西以相同的顺序传递给名为collect2 的中间实用程序,然后该实用程序调用实际的链接器ld。

请注意,如果 netCDF 库存档的共享对象版本也存在(即有 libnetcdff.so 和 libnetcdf.so),那么链接器会更喜欢静态存档(除非使用 @987654333 启用静态链接@ 选项),最后的链接阶段将处理给运行时链接编辑器(RTLD)(Ubuntu 上的/lib64/ld-linux-x86-64.so.2)。在这种情况下,与您的问题相同的命令行实际上会成功而不会出现链接错误,尽管这两个库都位于引用它们的代码之前,因为丢失的符号引用将由 RTLD 在加载可执行文件时解决文件。

【讨论】:

  • 有趣。但有一件事:/usr/lib/libnetcdf.so 和 /usr/lib/libnetcdff.so 已安装在我的系统上,所以你有没有想过为什么不使用它们?
  • 老实说,根本不知道。这可能是同时在 GCC collect2 实用程序中引入的一些更改。在 Ubuntu 8.04 和 10.04 上,我必须指定 -static 才能重现您的错误。你有什么 Ubuntu 版本,你有什么 gfortran 版本? 4.6 和 4.7 分支中引入了一些更改(10.04 附带 4.4.3)。
  • 很高兴知道。我有 Ubuntu 12.10 和 gfortran 4.7.2。
【解决方案2】:

在 Ubuntu 12.10 中,库的顺序是诀窍(正如 Hristo 建议的那样):


angelv@palas:~$ gfortran  -o xy -I/usr/include/ -L/usr/lib/  -lnetcdf -lnetcdff simple_xy_wr.f90
/tmp/ccj95anF.o: In function `check.1847':
simple_xy_wr.f90:(.text+0x72): undefined reference to `__netcdf_MOD_nf90_strerror'
/tmp/ccj95anF.o: In function `MAIN__':
simple_xy_wr.f90:(.text+0x284): undefined reference to `__netcdf_MOD_nf90_create'
simple_xy_wr.f90:(.text+0x2b6): undefined reference to `__netcdf_MOD_nf90_def_dim'
simple_xy_wr.f90:(.text+0x2e8): undefined reference to `__netcdf_MOD_nf90_def_dim'
simple_xy_wr.f90:(.text+0x432): undefined reference to `__netcdf_MOD_nf90_def_var_manydims'
simple_xy_wr.f90:(.text+0x468): undefined reference to `__netcdf_MOD_nf90_enddef'
simple_xy_wr.f90:(.text+0x4aa): undefined reference to `__netcdf_MOD_nf90_put_var_2d_fourbyteint'
simple_xy_wr.f90:(.text+0x4cb): undefined reference to `__netcdf_MOD_nf90_close'
collect2: error: ld returned 1 exit status

angelv@palas:~$ gfortran  -o xy -I/usr/include/ simple_xy_wr.f90 -L/usr/lib/  -lnetcdf -lnetcdff 

angelv@palas:~$ ./xy 
           0          12          24          36
 *** SUCCESS writing example file simple_xy.nc! 

【讨论】:

    猜你喜欢
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多