LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 852|回复: 5

使用库函数出现的怪问题,不知道是什么原因

[复制链接]
发表于 2003-10-24 15:37:17 | 显示全部楼层 |阅读模式
使用库函数出现的怪问题,不知道是什么原因
程序如下
#include <semaphore.h>
#include <sys/types.h>
#inlcude <unistd.h>


int main(void)
{
    sem_t sem;
    sem = sem_open("/tmp/myown", o_RDWR | O_EXCL,O_IRSUR,1);
    sem_close(sem);
}

编译竟然说sem_open sem_close 没有定义。
我在/usr/include 找到了这个文件里面有sem_open 和sem_close的声明。不知为什么会出错

RedHat 8。0
发表于 2003-10-24 16:03:01 | 显示全部楼层
O_IRSUR 这个是在那定义的?
发表于 2003-10-24 18:29:01 | 显示全部楼层

  1. #include <sys/types.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>

  5. #include <unistd.h>
  6. #include <semaphore.h>

  7. int main(int argc , char ** argv)
  8. {
  9.     sem_t* sem;
  10.     sem = sem_open("/tmp/myown", O_RDWR | O_EXCL,1, 1);
  11.     sem_close(sem);
  12.     return 1;
  13. }
复制代码


问题
1. 没有包含足够的 头文件
2. O_IRSUR 我也没有找到。
3. main 函数没有返回值
4. main 函数不符合标准格式,尽管无关紧要,但是养成好习惯
是重要的。
5. 应该是 sem_t *  ,不是 sem_t
 楼主| 发表于 2003-10-24 21:35:42 | 显示全部楼层
这几个头文件 我都包含了
结果编译还是报错说
sem_open 没有定义
 楼主| 发表于 2003-10-25 10:57:02 | 显示全部楼层
原程序是这样的
/*semacreate.c*/
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <semaphore.h>

#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)


int main(int argc, char **argv)
{
  int c, flags;
  sem_t *sem;
  unsigned int value;

  flags = O_RDWR | O_CREAT;
  value = 1;

  sem = sem_open("\\tmp\\my", flags, FILE_MODE, value);


  sem_close(sem);

  return (0);
}

编译成目标代码不会出错。gcc -c  semacreate.c

但是连接成可执行文件 就出错
gcc semacreate.c -o semacreate
/tmp/cc4Wrbww.o: In function 'main':
/tmp/cc4Wrbww.o(.tex + 0xae):undefined reference to 'sem_open'
/tmp/cc4Wrbww.o(.tex + 0xbf):undefined reference to 'sem_close'
collect2:ld returned 1 exit status

这到底是怎么回事 哪位告诉我阿
发表于 2003-10-25 11:07:17 | 显示全部楼层
编译时加上 -lpthread 这个必需连接线程库
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表