当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 3 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : [问题]连接外部库,C语言程序编译无法通过!
帖子发表于 : 2008-06-16 9:07 

注册: 2006-09-30 8:22
帖子: 52
送出感谢: 0 次
接收感谢: 0 次
调用glpk库进行线性规划求解的C程序,这是它的例程:
代码:
/* example.c*/

#include <stdio.h>
#include <stdlib.h>
#include <glpk.h>

int main(void)
{
   glp_prob *lp;
   int ia[1+1000],ja[1+1000];
   double ar[1+1000],z,x1,x2,x3;
   
   lp = glp_create_prob();
   glp_set_prob_name(lp,"sample");
   glp_set_obj_dir(lp,GLP_MAX);
   
   glp_add_rows(lp,3);
   
   glp_set_row_name(lp,1,"p");
   glp_set_row_bnds(lp,1,GLP_UP,0.0,100.0);
   
   glp_set_row_name(lp,2,"q");
   glp_set_row_bnds(lp,2,GLP_UP,0.0,600.0);
   
   glp_set_row_name(lp,3,"r");
   glp_set_row_bnds(lp,3,GLP_UP,0.0,300.0);
   
   glp_add_rows(lp,3);
   
   glp_set_col_name(lp,1,"x1");
   glp_set_col_bnds(lp,1,GLP_LO,0.0,0.0);
   glp_set_obj_coef(lp,1,10.0);
   
   glp_set_col_name(lp,2,"x2");
   glp_set_col_bnds(lp,2,GLP_LO,0.0,0.0);
   glp_set_obj_coef(lp,2,6.0);
   
   glp_set_col_name(lp,3,"x3");
   glp_set_col_bnds(lp,3,GLP_LO,0.0,0.0);
   glp_set_obj_coef(lp,3,4.0);
   
   ia[1] = 1,ja[1] = 1,ar[1] = 1.0;/* a[1,1] = 1*/
   ia[2] = 1,ja[2] = 2,ar[2] = 1.0;/* a[1,2] = 1*/
   ia[3] = 1,ja[3] = 3,ar[3] = 1.0;/* a[1,3] = 1*/
   ia[4] = 2,ja[4] = 1,ar[4] = 10.0;/* a[2,1] = 10*/
   ia[5] = 3,ja[5] = 1,ar[5] = 2.0;/* a[3,1] = 2*/
   ia[6] = 2,ja[6] = 2,ar[6] = 4.0;/* a[2,2] = 4*/
   ia[7] = 3,ja[7] = 2,ar[7] = 2.0;/* a[3,2] = 2*/
   ia[8] = 2,ja[8] = 3,ar[8] = 5.0;/* a[2,3] = 5*/
   ia[9] = 3,ja[9] = 3,ar[9] = 6.0;/* a[3,3] = 6*/
   
   glp_load_matrix(lp,9,ia,ja,ar);
   glp_simplex(lp,NULL);
   
   z = glp_get_obj_val(lp);
   x1 = glp_get_col_prim(lp,1);
   x2 = glp_get_col_prim(lp,2);
   x3 = glp_get_col_prim(lp,3);
   
   printf("\nz = %g; x1= %g; x2 = %g; x3 = %g\n",z,x1,x2,x3);
   glp_delete_prob(lp);
   
   return 0;
   
}


第一次编译提示:
引用:
hxl@superhxl:~/glpk$ gcc -Wall example.c -o example.out
/tmp/ccYbPgX0.o: In function `main':
example.c:(.text+0x15): undefined reference to `glp_create_prob'
example.c:(.text+0x2b): undefined reference to `glp_set_prob_name'
example.c:(.text+0x3e): undefined reference to `glp_set_obj_dir'
example.c:(.text+0x51): undefined reference to `glp_add_rows'
example.c:(.text+0x6c): undefined reference to `glp_set_row_name'
example.c:(.text+0x97): undefined reference to `glp_set_row_bnds'
example.c:(.text+0xb2): undefined reference to `glp_set_row_name'
example.c:(.text+0xdd): undefined reference to `glp_set_row_bnds'
example.c:(.text+0xf8): undefined reference to `glp_set_row_name'
example.c:(.text+0x123): undefined reference to `glp_set_row_bnds'
example.c:(.text+0x136): undefined reference to `glp_add_rows'
example.c:(.text+0x151): undefined reference to `glp_set_col_name'
example.c:(.text+0x178): undefined reference to `glp_set_col_bnds'
example.c:(.text+0x195): undefined reference to `glp_set_obj_coef'
example.c:(.text+0x1b0): undefined reference to `glp_set_col_name'
example.c:(.text+0x1d7): undefined reference to `glp_set_col_bnds'
example.c:(.text+0x1f4): undefined reference to `glp_set_obj_coef'
example.c:(.text+0x20f): undefined reference to `glp_set_col_name'
example.c:(.text+0x236): undefined reference to `glp_set_col_bnds'
example.c:(.text+0x253): undefined reference to `glp_set_obj_coef'
example.c:(.text+0x398): undefined reference to `glp_load_matrix'
example.c:(.text+0x3ab): undefined reference to `glp_simplex'
example.c:(.text+0x3b6): undefined reference to `glp_get_obj_val'
example.c:(.text+0x3cc): undefined reference to `glp_get_col_prim'
example.c:(.text+0x3e2): undefined reference to `glp_get_col_prim'
example.c:(.text+0x3f8): undefined reference to `glp_get_col_prim'
example.c:(.text+0x42e): undefined reference to `glp_delete_prob'
collect2: ld 返回 1

通过学习前辈贴和置顶贴,知道这是因为连接外部库的原因,所以重新进行编译:
引用:
hxl@superhxl:~/glpk$ gcc -Wall example.c /usr/lib/libglpk.a -o example.out
/usr/lib/libglpk.a(glplpx02.o): In function `_glp_lpx_put_mip_soln':
(.text+0x373): undefined reference to `floor'
/usr/lib/libglpk.a(glplpx04.o): In function `gm_scal':
(.text+0x821): undefined reference to `sqrt'
/usr/lib/libglpk.a(glplpx04.o): In function `gm_scal':
(.text+0x89b): undefined reference to `sqrt'
/usr/lib/libglpk.a(glpapi08.o): In function `glp_intopt':
(.text+0x583): undefined reference to `floor'
/usr/lib/libglpk.a(glpapi08.o): In function `glp_intopt':
(.text+0x5fd): undefined reference to `floor'
/usr/lib/libglpk.a(glpapi08.o): In function `glp_intopt':
(.text+0x635): undefined reference to `floor'
/usr/lib/libglpk.a(glpios03.o): In function `efficacy':
(.text+0x245): undefined reference to `sqrt'
/usr/lib/libglpk.a(glpios03.o): In function `efficacy':
(.text+0x26c): undefined reference to `sqrt'
/usr/lib/libglpk.a(glpios03.o): In function `branch_on':
(.text+0x39c): undefined reference to `floor'
/usr/lib/libglpk.a(glpios03.o): In function `branch_on':
(.text+0x44b): undefined reference to `ceil'
/usr/lib/libglpk.a(glpios03.o): In function `branch_drtom':
(.text+0x9f5): undefined reference to `ceil'
/usr/lib/libglpk.a(glpios03.o): In function `branch_drtom':
(.text+0xc77): undefined reference to `floor'
/usr/lib/libglpk.a(glpios03.o): In function `branch_drtom':
(.text+0xdf2): undefined reference to `floor'
/usr/lib/libglpk.a(glpios03.o): In function `branch_drtom':
(.text+0xe24): undefined reference to `ceil'
/usr/lib/libglpk.a(glpios03.o): In function `branch_drtom':
(.text+0xed6): undefined reference to `floor'
/usr/lib/libglpk.a(glpios03.o): In function `_glp_ios_driver':
(.text+0x18b1): undefined reference to `floor'
/usr/lib/libglpk.a(glpios03.o): In function `_glp_ios_driver':
(.text+0x18f6): undefined reference to `floor'
/usr/lib/libglpk.a(glpios03.o): In function `_glp_ios_driver':
(.text+0x1910): undefined reference to `ceil'
/usr/lib/libglpk.a(glpios03.o): In function `_glp_ios_driver':
(.text+0x2bd4): undefined reference to `floor'
/usr/lib/libglpk.a(glpios03.o): In function `_glp_ios_driver':
(.text+0x300a): undefined reference to `sqrt'
/usr/lib/libglpk.a(glpios03.o): In function `_glp_ios_driver':
(.text+0x34e2): undefined reference to `floor'
/usr/lib/libglpk.a(glpios03.o): In function `_glp_ios_driver':
(.text+0x34f6): undefined reference to `ceil'
/usr/lib/libglpk.a(glpios03.o): In function `_glp_ios_driver':
(.text+0x3554): undefined reference to `floor'
/usr/lib/libglpk.a(glpios03.o): In function `_glp_ios_driver':
(.text+0x3568): undefined reference to `ceil'
/usr/lib/libglpk.a(glpios03.o): In function `_glp_ios_driver':
(.text+0x362c): undefined reference to `floor'
/usr/lib/libglpk.a(glpios05.o): In function `_glp_ios_gmi_gen':
(.text+0x10b): undefined reference to `floor'
/usr/lib/libglpk.a(glpios05.o): In function `_glp_ios_gmi_gen':
(.text+0x222): undefined reference to `floor'
/usr/lib/libglpk.a(glpios05.o): In function `_glp_ios_gmi_gen':
(.text+0x642): undefined reference to `floor'
/usr/lib/libglpk.a(glpios05.o): In function `_glp_ios_gmi_gen':
(.text+0x663): undefined reference to `floor'
/usr/lib/libglpk.a(glpios06.o):(.text+0x139): more undefined references to `floor' follow
/usr/lib/libglpk.a(glpios02.o): In function `_glp_ios_preprocess_node':
(.text+0xfd6): undefined reference to `ceil'
/usr/lib/libglpk.a(glpios02.o): In function `_glp_ios_preprocess_node':
(.text+0x1000): undefined reference to `floor'
/usr/lib/libglpk.a(glpios02.o): In function `_glp_ios_preprocess_node':
(.text+0x1169): undefined reference to `ceil'
/usr/lib/libglpk.a(glpios02.o): In function `_glp_ios_preprocess_node':
(.text+0x1186): undefined reference to `floor'
collect2: ld 返回 1


不知道这是什么原因,似乎是库里面又链接了外部库,不知如何解决,谢谢!


页首
 用户资料  
 
2 楼 
 文章标题 :
帖子发表于 : 2008-06-16 9:23 
头像

注册: 2006-04-12 20:05
帖子: 8495
地址: 杭州
送出感谢: 0 次
接收感谢: 8
先确保装了 libglpk-dev 这个包

代码:
gcc -Wall example.c -lglpk -o example.out


_________________
关注我的blog: ε==3


页首
 用户资料  
 
3 楼 
 文章标题 :
帖子发表于 : 2008-06-16 10:34 

注册: 2006-09-30 8:22
帖子: 52
送出感谢: 0 次
接收感谢: 0 次
收到,谢谢!


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 3 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:Sogou [Spider] 和 3 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译