pthread中未定义的引用
undefined reference in pthread
我有一个无法解决的错误。我不明白为什么引用没有定义。pthread_attr_t早些时候被初始化为a1,根据参数,我认为它应该是正确的。这是我的代码和错误。谢谢你帮我解决这个烂摊子。
#include <iostream>
#include "buffer.h"
#include <pthread.h>
#include <semaphore.h>
using namespace std;
sem_t sem_mutex;
/* create the semaphore */
//sem_init(&sem_mutex, 0, 1);
/* acquire the semaphore */
//sem_wait(&sem_mutex);
/*** critical section ***/
/* release the semaphore */
//sem_post(&mutex);
int insert(buffer_item item);
int remove(buffer_item *item);
//void *thread_entry(void *param);
//create_mutex_lock(&mutex, NULL);
//pthread_mutex_lock(&mutex);
//release_mutex_lock(&mutex);
int insert(buffer_item item)
{
bool added=false;
/* insert an item into buffer */
item=item+1;
cout<<"The producer added "<<item<<endl;
if (added==true)// return 0 if successful, otherwise
return 0; //return -1 indicating an error condition */
else
return -1;
}
int remove(buffer_item *item)
{
bool removed=false;
item=item-1;/* remove an object from buffer and placing it in item*/
cout<<"the consumer removed "<< item<<endl;
if(removed==true)
return 0;// return 0 if successful
else
return -1;//otherwise return -1 indicating an error condition
}
void *thread_entry(void *param)
{ /* the entry point of a new thread */
pthread_t new_entry;
}
/* create the mutex lock */
//create_mutex_lock(&mutex, NULL);
/* acquire the mutex lock */
//pthread_mutex_lock(&mutex);
/*** critical section ***/
/* release the mutex lock */
//release_mutex_lock(&mutex);
int main()
{
/* 1. Answer the three command lines argument */
pthread_t t1;
pthread_attr_t a1;
buffer_item buffer=0;/* 2. Initialize buffer, mutex, semaphores, and other global vars */
buffer_item mutex=0;
buffer_item semaphore=0;
insert(buffer);/* 3. Create producer thread(s) */
/* get the default attribute */
pthread_attr_init(&a1);
/* create a new thread */
pthread_create(&t1, &a1, thread_entry, NULL);
//remove(*buffer);/* 4. Create consumer thread(s) */
/* 5. Sleep */
/* 6. Destroy mutex and semaphores */
return 0; /* 7. Exit */
}
错误
[Linker error] undefined reference to `_imp__pthread_attr_init'
[Linker error] undefined reference to `_imp__pthread_create'
ld returned 1 exit status
感谢您提供的任何帮助!
您可能犯的错误是没有使用-lpthread
或-pthread
进行编译。
但这并不重要,因为C++11有本机线程
C++11在这里有一大堆并发特性,这些特性实际上比pthreads 更好用
请记住,您仍然需要与pthread
相关文章:
- 编译C++时未定义的引用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 对 Scalar ::Scalar() 的未定义引用
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 对复制 CTOR 和 CTOR 的未定义引用
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 右值引用未定义的行为
- 对"vtable for DigitalClock"的未定义引用 - 未定义对"DigitalClock::staticMetaObject"的引用 - Qt
- 函数类型到函数类型的引用:未定义的引用
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确
- 从c++调用Fortran子程序,链接时引用未定义
- 枚举引用未定义,但位于包含的头文件中