C语言 使用信号量的未定义引用问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23556042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined Reference issues using Semaphores
提问by TheFatness
I am playing around with using Semaphores, but I keep encountering Undefined Reference warnings, thus causing my code not to work. I pulled example code from a text, but was having issues with some of their syntax, so I went to POSIX's semaphore tutorial and changed things around to their syntax and as a result am now getting these reference errors.
我正在尝试使用信号量,但我不断遇到未定义的引用警告,从而导致我的代码无法工作。我从文本中提取了示例代码,但是他们的一些语法有问题,所以我去了 POSIX 的信号量教程,并改变了他们的语法,结果我现在得到了这些参考错误。
I may simply be overlooking something, but I cannot find it.
我可能只是忽略了一些东西,但我找不到它。
Errors:
错误:
Producers_Consumers.c:52: warning: return type of ‘main' is not ‘int'
/tmp/cceeOM6F.o: In function `producer':
Producers_Consumers.c:(.text+0x1e): undefined reference to `sem_init'
Producers_Consumers.c:(.text+0x3a): undefined reference to `sem_init'
Producers_Consumers.c:(.text+0x46): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x52): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x5e): undefined reference to `sem_post'
Producers_Consumers.c:(.text+0x6a): undefined reference to `sem_post'
/tmp/cceeOM6F.o: In function `consumer':
Producers_Consumers.c:(.text+0x7e): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x8a): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x96): undefined reference to `sem_post'
Producers_Consumers.c:(.text+0xa2): undefined reference to `sem_post'
collect2: ld returned 1 exit status
What I have (It may look a bit ugly due to the way I commented things out from my old method) I also know my adding method won't work, but I'll get to that when I fix my syntax issues:
我所拥有的(由于我从旧方法中注释出来的方式,它可能看起来有点难看)我也知道我的添加方法不起作用,但是当我修复语法问题时我会解决这个问题:
#include <stdio.h>
#include <semaphore.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#define N 10 //Number of slots in buffer
typedef int semaphore; //Semaphores ae a special kind of int
sem_t mutex; //Controls access to critical region 1
sem_t empty; //Counts empty buffer slots N
sem_t full; //Counts full buffer slots 0
int count = 0; //What we're putting in
//int buffer[N];
void producer(void) {
sem_init(&mutex, 0, 1);
//sem_init(&empty, 0, N);
sem_init(&full, 0, 0);
while(1) {
sem_wait(&empty);
sem_wait(&mutex);
//printf("Empy: %d\n",empty);
//printf("Mutex: %d\n",mutex);
//printf("Both Downs Ran\n");
//buffer = buffer + 1;
sem_post(&mutex);
sem_post(&full);
//printf("Producer produced: %d\n",buffer);
}
}
void consumer(void) {
while(1) {
sem_wait(&full);
sem_wait(&mutex);
//item = buffer;
sem_post(&mutex);
sem_post(&empty);
//printf("Consumer consumed: %d/n",item);
}
}
void main() {
}
回答by Ryan Haining
If you are on a linux system, you'll need to compile and link with the -pthreadflag to link the pthreads library.
如果您使用的是 linux 系统,则需要编译和链接-pthread标志以链接 pthreads 库。
gcc -pthread Producers_Consumers.c
As Paul Griffiths has pointed out, you can also use -lrt, which is more portable, and links the POSIX Realtime Extensions library
正如 Paul Griffiths 指出的那样,您还可以使用-lrt更便携的 ,并链接 POSIX Realtime Extensions 库
gcc Producers_Consumers.c -lrt
int main(void)notvoid main()typedef int semaphoreis wrong,sem_tshould be treated as an opaque type, you never use this typedef in your code anyway.- A problem I foresee is that your
consumercode uses the semaphores before they are initialized inproducer. You should initialize them in yourmain
int main(void)不是void main()typedef int semaphore是错误的,sem_t应该被视为不透明类型,无论如何你永远不会在你的代码中使用这个 typedef。- 我预见的一个问题是您的
consumer代码在producer. 你应该在你的main
回答by Alexey
Got same error in ubuntu qt. After adding
在 ubuntu qt 中遇到同样的错误。添加后
LIBS += -lpthread -lrt
LIBS += -lpthread -lrt
to project.pro file all compiled fine.
到 project.pro 文件都编译得很好。

