码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-06 17:51:01      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:gcc   linux   c-c++   

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() 问题的解决方法

标签:gcc   linux   c-c++   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!