fortran - 对 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
解决方案
根据我在 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 选项指向它的位置。
推荐阅读
- docker-compose - 查找在 docker swarm 中运行的 docker 容器的 IP 地址
- javascript - Array.find 返回 TypeError / 正确使用参数
- sparql - 在 sparql 中获取可变数量的输出列
- spf - 关于首次创建 SPF 记录并尽量不中断邮件传递的问题
- javascript - ionic 'DocumentScanner' 指的是一个值,但在这里被用作一个类型
- c - 如何使用 printf("\b") 使控制台输出看起来像打字机?
- google-cloud-run - 无法让 HTTP PATCH 在 Google Cloud Run 实例上运行
- java - (正则表达式)如果数字是重复数字,我如何将数字转换为罗马形式
- azure - Azure Pipelines Blob 存储 URL
- docker - docker ADD --chown 错误或功能?