生如夏花bld 2015-12-29 12:44 采纳率: 100%
浏览 7161
已采纳

windows下使用pthread.h库的问题

#include
#include
#include
#include
#include
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t empty = PTHREAD_COND_INITIALIZER;
pthread_cond_t full = PTHREAD_COND_INITIALIZER;
char buf[256];

int main()
{
pthread_t t1,t2;
void * put_buf(void *);
void * get_buf(void *);
pthread_mutex_lock(&lock);
pthread_cond_signal(&empty);
pthread_create(&t1, NULL, put_buf, NULL);
pthread_create(&t2, NULL, get_buf, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
void *put_buf()
{
while (true)
{
pthread_cond_wait(&empty, &lock);
printf_s("empty flag is raised\n");
pthread_mutex_lock(&lock);
printf_s("input: \n");
gets_s(buf);
pthread_mutex_unlock(&lock);
pthread_cond_signal(&full);

}

}
void *get_buf(){
Sleep(2);
while (true)
{
pthread_cond_wait(&full, &lock);
printf_s("full flag is raised");
pthread_mutex_lock(&lock);
printf_s("output:\n");
pthread_cond_signal(&empty);
}
}


出现问题:错误 1 error LNK2019: 无法解析的外部符号 "void * __cdecl put_buf(void *)" (?put_buf@@YAPAXPAX@Z),该符号在函数 _main 中被引用 c:\Users\陌桑时代shine\documents\visual studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\源.obj ConsoleApplication2
错误 2 error LNK2019: 无法解析的外部符号 "void * __cdecl get_buf(void *)" (?get_buf@@YAPAXPAX@Z),该符号在函数 _main 中被引用 c:\Users\陌桑时代shine\documents\visual studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\源.obj ConsoleApplication2

  • 写回答

2条回答 默认 最新

  • 一枪尽骚丶魂 2015-12-30 00:55
    关注

    额,你要不就把函数写在main函数前面,要不就写上该函数的声明!二选其一呀

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?