2

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

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

~/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_solve库没有加载到我的代码中。您能告诉我如何正确链接lp_solve库吗?

4

1 回答 1

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 
于 2013-10-11T02:04:00.613 回答