关于shm_open和shm_unlink的使用问题 undefined reference to

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 -o test test.c -lrt

其实就是要连接库的原因。但是需要注意,-lrt需要放到后面,如果放到前面,还会报同样的错误,原因未知。下面给出一个我测试过的例子

set.c

#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define STORAGE_ID "/SHM_TEST"
#define STORAGE_SIZE 32
#define DATA "Hello, World! From PID %d"

int main(int argc, char *argv[])
{
	int res;
	int fd;
	int len;
	pid_t pid;
	void *addr;
	char data[STORAGE_SIZE];

	pid = getpid();
	sprintf(data, DATA, pid);

	// get shared memory file descriptor (NOT a file)
	fd = shm_open(STORAGE_ID, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
	if (fd == -1)
	{
		perror("open");
		return 10;
	}

	// extend shared memory object as by default it's initialized with size 0
	res = ftruncate(fd, STORAGE_SIZE);
	if (res == -1)
	{
		perror("ftruncate");
		return 20;
	}

	// map shared memory to process address space
	addr = mmap(NULL, STORAGE_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
	if (addr == MAP_FAILED)
	{
		perror("mmap");
		return 30;
	}

	// place data into memory
	len = strlen(data) + 1;
	memcpy(addr, data, len);

	// wait for someone to read it
	sleep(2);

	// mmap cleanup
	res = munmap(addr, STORAGE_SIZE);
	if (res == -1)
	{
		perror("munmap");
		return 40;
	}

	// shm_open cleanup
	fd = shm_unlink(STORAGE_ID);
	if (fd == -1)
	{
		perror("unlink");
		return 100;
	}

	return 0;
}

get.c

#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define STORAGE_ID "/SHM_TEST"
#define STORAGE_SIZE 32

int main(int argc, char *argv[])
{
	int res;
	int fd;
	char data[STORAGE_SIZE];
	pid_t pid;
	void *addr;

	pid = getpid();

	// get shared memory file descriptor (NOT a file)
	fd = shm_open(STORAGE_ID, O_RDONLY, S_IRUSR | S_IWUSR);
	if (fd == -1)
	{
		perror("open");
		return 10;
	}

	// map shared memory to process address space
	addr = mmap(NULL, STORAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
	if (addr == MAP_FAILED)
	{
		perror("mmap");
		return 30;
	}

	// place data into memory
	memcpy(data, addr, STORAGE_SIZE);

	printf("PID %d: Read from shared memory: \"%s\"\n", pid, data);

	return 0;
}

run.sh

#!/bin/bash

function is_linux
{
	if [[ "$(uname)" == "Linux" ]]; then
		echo 1
		return
	fi

	echo 0
}

SET="set"
GET="get"
CFLAGS=""
if [[ "$(is_linux)" == "1" ]]; then
	CFLAGS="-lrt"
fi

cc  -o ${SET} ${SET}.c ${CFLAGS}
cc  -o ${GET} ${GET}.c ${CFLAGS}

./${SET} &
sleep 1
./${GET}
if [[ "$(is_linux)" == "1" ]]; then
	echo "/dev/shm:"
	ls -l /dev/shm
fi
sleep 1

rm ${SET} ${GET}

 

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值