尝试在 Linux 上的 C/C++ 中使用 lp_solve 时出现“未定义的引用”

"undefined reference" when trying to use lp_solve in C/C++ on Linux

提问人:zambro 提问时间:10/11/2013 最后编辑:einpoklumzambro 更新时间:12/20/2016 访问量:2481

问:

我是线性规划和 C 语言的新手。我正在尝试使用该库来求解线性方程。问题是我无法让演示代码工作。lp_solve

文档中所述,我下载到我的 Linux 机器上。我将文件解压缩到与代码相同的文件夹中,并尝试编译:lp_solve_5.5_dev.gz

~/Desktop/Code/CA$ cc -o demo demo.c
/tmp/ccpeN8ZV.o: In function `demo':
demo.c:(.text+0x31): undefined reference to `make_lp'
demo.c:(.text+0x62): undefined reference to `set_col_name'
demo.c:(.text+0x7d): undefined reference to `set_col_name'
demo.c:(.text+0xcf): undefined reference to `set_add_rowmode'
demo.c:(.text+0x150): undefined reference to `add_constraintex'
demo.c:(.text+0x1e6): undefined reference to `add_constraintex'
demo.c:(.text+0x274): undefined reference to `add_constraintex'
demo.c:(.text+0x29c): undefined reference to `set_add_rowmode'
demo.c:(.text+0x30b): undefined reference to `set_obj_fnex'
demo.c:(.text+0x327): undefined reference to `set_maxim'
demo.c:(.text+0x33b): undefined reference to `write_LP'
demo.c:(.text+0x34e): undefined reference to `set_verbose'
demo.c:(.text+0x359): undefined reference to `solve'
demo.c:(.text+0x383): undefined reference to `get_objective'
demo.c:(.text+0x3a6): undefined reference to `get_variables'
demo.c:(.text+0x3d2): undefined reference to `get_col_name'
demo.c:(.text+0x429): undefined reference to `delete_lp'
collect2: ld returned 1 exit status

当我添加到命令时,出现以下错误:liblpsolve55.a

~/Desktop/Code/CA$ cc -o demo demo.c liblpsolve55.a
liblpsolve55.a(lp_lib.o): In function `scaled_floor':
lp_lib.c:(.text+0x6923): undefined reference to `floor'
liblpsolve55.a(lp_lib.o): In function `scaled_ceil':
lp_lib.c:(.text+0x6f03): undefined reference to `ceil'
liblpsolve55.a(lp_lib.o): In function `set_XLI':
lp_lib.c:(.text+0x7aa2): undefined reference to `dlclose'
lp_lib.c:(.text+0x7bb3): undefined reference to `dlopen'
lp_lib.c:(.text+0x7bd1): undefined reference to `dlsym'
lp_lib.c:(.text+0x7d8a): undefined reference to `dlsym'
lp_lib.c:(.text+0x7da6): undefined reference to `dlsym'
lp_lib.c:(.text+0x7dc2): undefined reference to `dlsym'
liblpsolve55.a(lp_lib.o): In function `set_BFP':
lp_lib.c:(.text+0x7eb4): undefined reference to `dlclose'
lp_lib.c:(.text+0x7fc3): undefined reference to `dlopen'
lp_lib.c:(.text+0x7fe1): undefined reference to `dlsym'
lp_lib.c:(.text+0x831a): undefined reference to `dlsym'
lp_lib.c:(.text+0x8336): undefined reference to `dlsym'
lp_lib.c:(.text+0x8352): undefined reference to `dlsym'
lp_lib.c:(.text+0x836e): undefined reference to `dlsym'
liblpsolve55.a(lp_lib.o):lp_lib.c:(.text+0x838a): more undefined references to `dlsym' follow
................

我知道这些库没有加载到我的代码中。你能告诉我如何正确链接库吗?lp_solvelp_solve

c 静态库 链接器错误 未定义引用 lpsolve

评论

2赞 DanielKO 10/11/2013
您是否尝试过在网络上搜索“对'dlsym'的未定义引用”?你会很快找到你所缺少的东西。
0赞 zambro 10/11/2013
谢谢!不要知道头文件包含声明而不是函数定义。链接器似乎无法找到函数定义。

答:

1赞 vityav 10/11/2013 #1

您只是缺少一些编译器标志来包含正确的库。正如 danielko 所说,对缺失的库进行快速的谷歌搜索会带来一些建议。例如

Linux c++ 错误:未定义对“dlopen”的引用

http://sourceforge.net/p/dev-cpp/discussion/48211/thread/3bd8b8a1

这些建议你的编译应该更像:

cc -o demo demo.c -ldl -llpsolve55

lpsolve 文档建议使用包含项目目录的命令,例如:

cc -I/lp_solve_5.5 -I/lp_solve_5.5/bfp -I/lp_solve_5.5/bfp/bfp_etaPFI -I/lp_solve_5.5/colamd $src -o MyExe.exe -lm -ldl 

评论

1赞 zambro 10/11/2013
尽管您答案中的编译命令并不准确。我接受你的回答,因为你为我指明了正确的方向,谢谢你!使用以下方法解决了数学和lp_lib未定义的引用:cc -o demo demo.c liblpsolve55.a -lm -ldl
0赞 vityav 10/11/2013
很高兴它至少有部分帮助。我总是遇到这样的链接器问题,它总是只是一项任务,即找到需要添加哪些库。