关于:(.text+0x21): undefined reference to `shm_open'问题
来源:互联网 发布:温州软件外派 编辑:程序博客网 时间:2024/04/27 14:48
C programming in the UNIX environment的编程手册,一般都会为进程间用共享内存的方法通信提供两组方法:
1. POSIX定义的:
int shm_open(const char *name, int oflag, mode_t mode);
int shm_unlink(const char *name);
int ftruncate(int fd, off_t length);
2. SYSTEM V定义的
int shmget(key_t key, int size, int shmflg);
void *shmat(int shmid, const void *shmaddr, int shmflg);
int shmdt(const void *shmaddr);
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
由于POSIX标准比较通用,一般建议使用该标准定义的方法集。
但是在使用shm_open和shm_unlink两个函数时,你可能遇到和我同样的问题,见如下代码。
该代码旨在测试你的系统是否支持POSIX定义的共享内存函数集。
/* This is just to test if the function is found in the libs. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
int
main (void)
{
int i;
i = shm_open ("/tmp/shared", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
printf ("shm_open rc = %d/n", i);
shm_unlink ("/tmp/shared");
return (0);
}
假设它所在的文件为"test.c"
我这么编译:
gcc -o test test.c
结果为:
/tmp/ccaGhdRt.o(.text+0x23): In function `main':
: undefined reference to `shm_open'
/tmp/ccaGhdRt.o(.text+0x49): In function `main':
: undefined reference to `shm_unlink'
collect2: ld returned 1 exit status
编译结果实际上是说,没include相应的头文件,或是头文件不存在(即系统不支持该库函数)
但我man shm_open是可以找到帮助文件的(说明系统支持),原因何在???
请注意一下man shm_open的帮助文件的最后几行:
NOTES
These functions are provided in glibc 2.2 and later. Programs using
these functions must specify the -lrt flag to cc in order to link
against the required ("realtime") library.
POSIX leaves the behavior of the combination of O_RDONLY and O_TRUNC
unspecified. On Linux, this will successfully truncate an existing
shared memory object - this may not be so on other Unices.
The POSIX shared memory object implementation on Linux 2.4 makes use of
a dedicated file system, which is normally mounted under /dev/shm.
如果你注意到的话,这样编译就能通过了:
gcc -lrt -o test test.c
其实就是要连接库的原因。- 关于:(.text+0x21): undefined reference to `shm_open'问题
- 程序报错 undefined reference to `shm_open'
- 关于demo.cpp:(.text.startup+0x8f): undefined reference to `vtable for SlotObject' ,问题探究
- (.text+0xb3a): undefined reference to `clock_gettime'
- (.text+0x11): undefined reference to `mysql_init'
- (.text+0x168): undefined reference to `uart0_init'
- (.text+0x159d):undefined reference to `va_start'
- thread_join.c:(.text+0x6f): undefined reference to `pthread_create' 问题的解决
- Bug:__uClibc_main.c:(.text+0x20): undefined reference to `__fini_array_start'问题的解决
- limit.c:(.text+0xa5): undefined reference to `sin'
- (.text.startup+0x...): undefined reference to ... 原因之一
- solution :(.text+0x18c1): undefined reference to `uuid_unparse'
- busybox mount.c:(.text+0x42c): undefined reference to `del_loop'
- test.cpp:(.text+0x66): undefined reference to `event_init'
- hello.c:(.text+0x45): undefined reference to `XDisplayName'
- 解决 init.c:(.text+0x34): undefined reference to `main'
- (.text+0x2f0f): undefined reference to ‘函数名’
- gcc xx.c:(.text+0x11): undefined reference to `sqrt'
- Spring 3.0 注解注入详解
- 黄淮学院CSDN高校俱乐部第一次c语言培训
- 关于RDF的技术支持和应用部分示例
- 【菜鸟入门】stm32的第一个程序--LED
- codeforces Round 211 Div 2部分题解(持续更新中.......)
- 关于:(.text+0x21): undefined reference to `shm_open'问题
- 下图片地图有关
- 黑马程序员-为什么要设置JAVA_HOME、Path、CLASSPATH、CATALINA_HOME
- Opencv问题:Bad argument can not get new positive sample
- MyEclipse 启动报错:'Building workspace' has encountered a problem解决方法
- <操作系统>进程与线程的有趣理解
- <操作系统>进程与线程的有趣理解
- Scala 设计模式:行为型模式
- HDU 2874 LCA转RMQ裸题