注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

gmd20的个人空间

// 编程和生活

 
 
 

日志

 
 

gcc的链接选项-llibrary 执行链接库是有顺序的  

2014-06-01 17:05:11|  分类: linux相关 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
编译程序时,发现一个奇怪的错误,明明有 -lz 指定了 zlib的链接库了。就是找不到zlib里面函数。
原来是因为 *.o  *.a写在 -lz的后面了。  这恶 -l选项指定库时是有顺序的。 只对它前面的那些 *.o文件起作用。
参考
https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
-llibrary
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.

The directories searched include several standard system directories plus any that you specify with -L.

Normally the files found this way are library files—archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -loption and specifying a file name is that -l surrounds librarywith ‘lib’ and ‘.a’ and searches several directories. 


所以 -l 这个选项最好放到命令的最后面。改成放到*.a之后就编译通过了。



  评论这张
 
阅读(389)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017