首页 > 解决方案 > 对 nlopt_... 符号的未定义引用

问题描述

我对 Fortran 相当陌生,这听起来像是一个愚蠢的问题。我在编译作为示例发布在 NLOPT Wiki 中的 Fortran 代码时遇到错误。

我使用以下命令在 Ubuntu 18.04 LTS 中编译:

gfortran example-nlopt.f90 -o example-nlopt -I/usr/local/include/

终端中产生以下错误:

/tmp/ccbAim6b.o: In function `MAIN__':
example-nlopt.f90:(.text+0x26): undefined reference to `nlo_create_'
example-nlopt.f90:(.text+0x42): undefined reference to `nlo_get_lower_bounds_'
example-nlopt.f90:(.text+0x67): undefined reference to `nlo_set_lower_bounds_'
example-nlopt.f90:(.text+0x8a): undefined reference to `nlo_set_min_objective_'
example-nlopt.f90:(.text+0xca): undefined reference to `nlo_add_inequality_constraint_'
example-nlopt.f90:(.text+0x10e): undefined reference to `nlo_add_inequality_constraint_'
example-nlopt.f90:(.text+0x12d): undefined reference to `nlo_set_xtol_rel_'
example-nlopt.f90:(.text+0x164): undefined reference to `nlo_optimize_'
example-nlopt.f90:(.text+0x305): undefined reference to `nlo_destroy_'
collect2: error: ld returned 1 exit status

标签: fortrannlopt

解决方案


根据我在 nlopt 的文档(https://nlopt.readthedocs.io/en/latest/NLopt_Installation/#sharing-the-installation-directory)中看到的内容,您似乎只需要指定要链接的库。也许试试这个:

gfortran  -I/usr/local/include/ -L/usr/local/lib example-nlopt.f90 -o example-nlopt -lnlopt -lm

这假设您在 /usr/local/lib 中有 libnlopt.so,如果没有,则使用 -L 选项指向它的位置。


推荐阅读