首页 > 其他 > 详细

gcc/g++ undefined reference to function() 问题的解决方法

时间:2015-07-06 17:50:21      阅读:580      评论:0      收藏:0      [点我收藏+]

g++ 编译一个程序g++ shm.c -o shm,
用到了 shm_open(), shm_unlink()函数,编译出现了下面的错误:

/tmp/ccL2cbha.o: In function `del_resource()‘:
shm.c:(.text+0x1b3): undefined reference to `shm_unlink‘
/tmp/ccL2cbha.o: In function `main‘:
shm.c:(.text+0x872): undefined reference to `shm_open‘
collect2: error: ld returned 1 exit status

后来翻阅shm_open()文档,赫然发现下面的内容:
在终端中输入 man shm_open 回车:

SYNOPSIS
       #include <sys/mman.h>
       #include <sys/stat.h>        /* For mode constants */
       #include <fcntl.h>           /* For O_* constants */

       int shm_open(const char *name, int oflag, mode_t mode);

       int shm_unlink(const char *name);

       Link with -lrt.

Link with -lrt 表明编译的时候要加上 -lrt 。
所以正确的编译命令应该是:
g++ shm.c -lrt -o shm

我想类似与于这种编译错误,好方法是看man文档,找是否需要显示链接其他库。

版权声明:本文为博主原创文章,未经博主允许不得转载。

gcc/g++ undefined reference to function() 问题的解决方法

原文:http://blog.csdn.net/jason___bourne/article/details/46776793

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!