undefined reference to `crypt‘

在Linux环境下使用GCC编译密码校验模块时遇到`undefined reference to `crypt'`错误。问题根源在于链接库选项`-l`的位置。将`-lcrypt`放在依赖它的源文件之后可以解决问题。了解到静态库和共享库的区别,并且链接器按顺序处理文件,所以库的位置影响到依赖函数是否能正确加载。
摘要由CSDN通过智能技术生成

才疏学浅,对C了解不多。Linux下尝试写一个密码校验模块时,编译出错。查了半天跟gcc链接选项位置有关,记录一下。

最初编译过程,报错:

gcc -lcrypt chkpwd.c -o chkpwd

/usr/bin/ld: /tmp/ccVl9cer.o: in function `main':
chkpwd.c:(.text+0x7d): undefined reference to `crypt'
collect2: error: ld returned 1 exit status
make: *** [<内置>:chkpwd] 错误 1

后来无意间执行成功了:

gcc chkpwd.c -o chkpwd -lcrypt

只是-l放置位置不同,前者报错,后者通过。

从StackOverflow以及gcc man手册查到原因:

-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.)

           The -l option is passed directly to the linker by GCC.  Refer to your linker documentation for exact details.  The general
           description below applies to the GNU linker.

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值