在Win7的MinGW中使用pthreads [英] Use pthreads in MinGW in Win7

查看:856
本文介绍了在Win7的MinGW中使用pthreads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2015-1-20,我在Win7中使用MinGW,尝试用MinGW和Pthreads编译我的源代码. MinGW已经支持POSIX线程,在"MinGW Installation Manager"中,我们可以安装pthreads dev软件包和pthreads lib. 但是,当我编译源代码时,会出现错误:重新定义struct timespec,首先在pthread.h中,然后在unistd.h中,我不知道为什么.

2015-1-20, I use MinGW in my Win7, try to compile my source code with MinGW and Pthreads. The MinGW has already support POSIX threads, In the "MinGW Installation Manager", we can install the pthreads dev package and pthreads lib. But when I compile my source codes, there's an error: struct timespec redefinition, first in pthread.h, then in unistd.h, I don't know why.

在此之前,实际上我安装的是不带pthreads dev和lib软件包的minGW,我下载了pthreads-for-win32源代码并在本地进行构建.我使用-I/somewhere -L/somewhere -lpthreadGC2链接到我的源代码,并且可以正常工作.

Before this, actually I install the minGW without the pthreads dev and lib package, I download the pthreads-for-win32 source code and build it locally. I use -I/somewhere -L/somewhere -lpthreadGC2 to link to my source code, and it worked.

但是这一次,它无法使用MinGW自包.我需要帮助. 我无法上传图片来显示详细信息....

But this time, it fail with MinGW self package. I need help. I cannot upload a picture to show you the details....

推荐答案

实际上,我找到了解决方案.

Actually, I find a solution.

不要在"pthread.h"之后包含"unistd.h",而应使用本地winapi.

Don't include "unistd.h" after "pthread.h", use local winapi instead.

在Linux中使用gcc使用"pthreads"编码很容易,但是Win7不支持,请使用"MinGW Installation Manager"(google"mingw"并下载)进行安装. 安装mingw时,在"MinGW Standard Library"中选择"mingw32-pthreads-win32"开发包,则无需下载pthreads-for-win32源代码或在本地构建它.但是"pthread.h"将与"unistd.h"发生冲突,在Windows中,我们应该使用Windows API而不是unix API.

It's easy to code with "pthreads" with gcc in Linux, but this is not support in Win7, use "MinGW Installation Manager" (google "mingw" and download) to install. Choose the "mingw32-pthreads-win32" dev package in "MinGW Standard Library" when you install the mingw, then you don't need to download pthreads-for-win32 source code or build it locally. But the "pthread.h" will conflict with "unistd.h", in Windows, we should use Windows API instead of unix API.

这篇关于在Win7的MinGW中使用pthreads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆