C Windows上的Pthread-对睡眠的未定义引用

C Windows上的Pthread-对睡眠的未定义引用,c,sleep,undefined-reference,pthreads-win32,C,Sleep,Undefined Reference,Pthreads Win32,我一直在尝试实现一个解决方案,使用pthreads,我尝试在我的Lenovo yoga2pro pc(运行在windows 8.1上)上运行它,尽管我通过mingw安装管理器安装了用于windows的pthreads,但我得到了以下错误:只有在代码的某些部分中存在sleep()方法时才会发生此错误 C:\Users\Anil\Desktop\Github>gcc dphi.c -pthread C:\Users\Anil\AppData\Local\Temp\ccAxhsF4.o:dphi

我一直在尝试实现一个解决方案,使用pthreads,我尝试在我的Lenovo yoga2pro pc(运行在windows 8.1上)上运行它,尽管我通过mingw安装管理器安装了用于windows的pthreads,但我得到了以下错误:只有在代码的某些部分中存在sleep()方法时才会发生此错误

C:\Users\Anil\Desktop\Github>gcc dphi.c -pthread
C:\Users\Anil\AppData\Local\Temp\ccAxhsF4.o:dphi.c:(.text+0x185): undefined refe
rence to `sleep'
C:\Users\Anil\AppData\Local\Temp\ccAxhsF4.o:dphi.c:(.text+0x1de): undefined refe
rence to `sleep'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\A
nil\AppData\Local\Temp\ccAxhsF4.o: bad reloc address 0x0 in section `.data'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link
 failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
当我试图编译另一个没有sleep()方法的文件时。这就是我得到的:

C:\Users\Anil\Desktop\Github>gcc dp.c -pthread
C:\Users\Anil\Desktop\Github>a.exe
Thread 1 says Hello!
Thread 2 says Hi!

它很好用。我刚刚开始编写这段代码,以了解更多关于线程的信息,因为我正在尝试将解决方案作为一个项目来实现。我不知道我做错了什么。(我尝试在windows上使用pthreads执行此操作的原因是因为这是一项要求)

如果我没有错,
sleep()
unistd.h
的一部分,而不是
pthread
。。。 在linux上:添加

#include <unistd.h>
#包括
(并在需要时链接相应的库)

在windows(您的案例)上,我不知道等效的方法,但请检查您是否具备所需的所有包含(和链接库)

简单方法:

#include <windows.h>
// Win32's Sleep() is in millis
#define sleep(n) Sleep(n*1000)
#define usleep(n) Sleep(n/1000)
#包括
//Win32的Sleep()以毫秒为单位
#定义睡眠(n)睡眠(n*1000)
#定义usleep(n)睡眠(n/1000)

@mnunberg是的,你是对的,我在重新检查(:我的include语法错误^^非常感谢:)我已经包含了,所以每当我尝试运行相同的代码时,我都会添加此代码段,当我尝试在终端上运行相同的东西时,我只会注释掉它。:)