【问题标题】:warning C4013: 'sleep' undefined; assuming extern returning int警告 C4013:“睡眠”未定义;假设 extern 返回 int
【发布时间】:2014-09-08 13:06:12
【问题描述】:

我的目标是从餐饮哲学家 C 程序中获得输出。 我在 Windows 7 上使用 Visual Studio 2013 来编译和执行 C 程序。 出现错误,指出 pthread.h、semaphore.h 不可用。 为 windows 构建下载了相同的内容并包含在项目中。

现在我收到以下 8 个错误

警告 C4013:“睡眠”未定义;假设 extern 返回 int

错误 LNK2019:引用了未解析的外部符号 __imp__sem_init 在函数_main中

错误 LNK2019:引用了未解析的外部符号 __imp__sem_wait 在函数_put_fork中

错误 LNK2019:引用了未解析的外部符号 __imp__sem_post 在函数_put_fork中

错误 LNK2019:未解析的外部符号 __imp__pthread_create 在函数_main中引用

错误 LNK2019:未解析的外部符号 __imp__pthread_join 在函数_main中引用

错误 LNK2019:未解析的外部符号 _sleep 在 函数_philosphere

错误 LNK1120:6 个未解决的外部问题

我使用的代码是`

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

#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N

sem_t mutex;
sem_t S[N];

void * philospher(void *num);
void take_fork(int);
void put_fork(int);
void test(int);

int state[N];
int phil_num[N] = { 0, 1, 2, 3, 4 };

int main()
{
    int i;
    pthread_t thread_id[N];
    sem_init(&mutex, 0, 1);
    for (i = 0; i<N; i++)
        sem_init(&S[i], 0, 0);
    for (i = 0; i<N; i++)
    {
        pthread_create(&thread_id[i], NULL, philospher, &phil_num[i]);
        printf("Philosopher %d is thinking\n", i + 1);
    }
    for (i = 0; i<N; i++)
        pthread_join(thread_id[i], NULL);
}

void *philospher(void *num)
{
    while (1)
    {
        int *i = num;
        sleep(1);
        take_fork(*i);
        sleep(0);
        put_fork(*i);
    }
}

void take_fork(int ph_num)
{
    sem_wait(&mutex);
    state[ph_num] = HUNGRY;
    printf("Philosopher %d is Hungry\n", ph_num + 1);
    test(ph_num);
    sem_post(&mutex);
    sem_wait(&S[ph_num]);
    sleep(1);
}

void test(int ph_num)
{
    if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
    {
        state[ph_num] = EATING;
        sleep(2);
        printf("Philosopher %d takes fork %d and %d\n", ph_num + 1, LEFT + 1, ph_num + 1);
        printf("Philosopher %d is Eating\n", ph_num + 1);
        sem_post(&S[ph_num]);
    }
}

void put_fork(int ph_num)
{
    sem_wait(&mutex);
    state[ph_num] = THINKING;
    printf("Philosopher %d putting fork %d and %d down\n", ph_num + 1, LEFT + 1, ph_num + 1);
    printf("Philosopher %d is thinking\n", ph_num + 1);
    test(LEFT);
    test(RIGHT);
    sem_post(&mutex);
}`

【问题讨论】:

  • 您是如何在 VSE 上安装 pthreads 的?
  • 您是否在属性中向链接器添加了依赖项?睡眠也没有定义,购买方式,我使用来自 windows.h 的 Sleep()
  • pthreads 特定于 POSIX。您不能只获取头文件的副本并期望它在 Windows 上工作。
  • 亲爱的 Ivan Ivanov,我从sourceware.org/pub/pthreads-win32/sources/…下载了
  • 亲爱的 Ivan Ivanov, 请帮助我在属性中添加对链接器的依赖项。也帮我定义睡眠。也请帮我找到适合windows7的windows.h

标签: visual-c++


【解决方案1】:

为时已晚,但今天我也遇到了这个问题。 问题: I can't use pthread in window platform。 解决方案:在 C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib 中找到 pthreadVC2.lib 的另一个变体(对我来说是 x86 而不是 x64)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    相关资源
    最近更新 更多