免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 11686 | 回复: 5
打印 上一主题 下一主题

关于gcc 创建静态库的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-10 22:30 |只看该作者 |倒序浏览
用 gcc 创建静态库的的时候,出了点问题。

写了两个源文件  hellofirst.c  hellosecond.c

/*hellofirst.c*/

#include <stdio.h>

void hellofirst()
  {printf("The first hello\n")  ;
  }

/*hellofirst.c*/

#include <stdio.h>

void hellosecond()
{printf("The second hello\n")  ;
  }


然后              ar -r libhello.a  hellofirst.c  hellosecond.c          创建一个名为 libhello.a的库
然后些一个源文件 twohellos.c

/*twohellos.c*/
void hellofirst();
void hellosecond();

int main(int argc,char *argv[])
{hellofirst();
hellosecond();
return(0);
}     

然后运行指定库  gcc twohellos.c libhello.a -o twohellos
出问题了,命令行下显示

libhello.a: could not read symbols: Archive has no index; run ranlib to add one
collect2: ld 返回 1

新手问题,多多包涵,多谢

[ 本帖最后由 比克流 于 2007-10-10 22:31 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-10-10 22:42 |只看该作者
ar -r libhello.a  hellofirst.c  hellosecond.c

最好别这么用.

先编译成.o文件,然后再用ar.

ar有个特点,就是如果 .c文件没编译过,它不给提示,直接将其它编译过的文件归档.

论坛徽章:
0
3 [报告]
发表于 2007-10-10 22:44 |只看该作者
原帖由 比克流 于 2007-10-10 22:30 发表
用 gcc 创建静态库的的时候,出了点问题。

写了两个源文件  hellofirst.c  hellosecond.c

/*hellofirst.c*/

#include

void hellofirst()
  {printf("The first hello\n")  ;
  }

/*hellofirs ...

静态库是这样打包的吗?
应该是先编译成.o文件,再打包吧

论坛徽章:
0
4 [报告]
发表于 2007-10-10 22:57 |只看该作者

论坛徽章:
0
5 [报告]
发表于 2007-10-12 11:48 |只看该作者
中间忘了打上    gcc  -c  hellofirst.c   hellosecond.c  , 这一步编译成 .o 的文件。
关于那个  ar -r 用法,是从那本《GCC 技术参考大全》上看的,没仔细搞清楚各个选项的含义就直接抄上了。
问题继续

论坛徽章:
0
6 [报告]
发表于 2007-10-12 11:58 |只看该作者
不知怎么回事,今天刚刚有把命令重新输一遍,运行成功了,惊喜阿,
谢谢各位 gg jj 帮忙
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP