【问题标题】:Semaphore symbols not found (c)未找到信号量符号 (c)
【发布时间】:2014-03-21 23:32:38
【问题描述】:

所以我做了一个使用信号量的方法。

我的程序打开

#include <pthread.h>
#include <semaphore.h>

我的make文件是

officeHours: officeHours.c
    gcc -o officeHours officeHours.c -lpthread -lrt

编辑:添加 -lrt....没有变化

但是,当我制作它找到的文件时,找不到符号

sem_destroy sem_getvalue sem_init sem_post sem_wait

这是为什么?

编辑:“make officeHours”的输出

cc -o officeHours officeHours.c
"officeHours.c", line 4: warning: invalid white space character in directive
"officeHours.c", line 5: warning: invalid white space character in directive
"officeHours.c", line 41: warning: argument #3 is incompatible with prototype:
    prototype: pointer to function(pointer to void) returning pointer to void : "/usr/include/pthread.h", line 197
    argument: pointer to void
"officeHours.c", line 68: warning: argument #3 is incompatible with prototype:
    prototype: pointer to function(pointer to void) returning pointer to void : "/usr/include/pthread.h", line 197
    argument: pointer to void
Undefined symbol            first referenced in file
sem_destroy                 officeHours.o
sem_getvalue                officeHours.o
sem_init                    officeHours.o
sem_post                    officeHours.o
sem_wait                    officeHours.o

【问题讨论】:

  • 如果您查看 Makefile 输出的第一行,您会发现它正在调用不同的命令:cc -o officeHours officeHours.c
  • 嗯.....它为什么要这么做?我的make文件和上面的完全一样
  • 另外,如果是这样的话,编译器不会也抱怨 pthread_create 和 pthread_join 因为它们来自 -lpthread

标签: c semaphore symbols


【解决方案1】:

您需要链接到librt 库。

此库包含 POSIX 实时扩展。

officeHours: officeHours.c
    gcc -o officeHours officeHours.c -lpthread -lrt

如果您查看 sem_init(3RT) 的手册页:

cc [ flag... ] file... -lrt [ library... ]
#include <semaphore.h>

int sem_init(sem_t *sem, int pshared, unsigned int value);

【讨论】:

  • 而行 gcc -o officeHours officeHours.c -lpthread -lrt 似乎在命令行中工作.....make文件似乎仍然没有变化
  • makefile 在原始注释中。我刚刚使用添加的 -lrt 进行了编辑
  • 你能发布整个 gcc/Make 输出吗?
  • k 我在那里为你添加了它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 2021-12-17
相关资源
最近更新 更多