如何为 Visual Studio 2005 安装 pthread_win32(Windows pthread/posix 线程库)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5654073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:38:03  来源:igfitidea点击:

How do I install pthread_win32 (Windows pthread / posix thread library) for Visual Studio 2005?

cwindowswinapivisual-studio-2005pthreads

提问by Sam Levin

Just to be clear - I have searched the depths of the internet and back for information on how to do this

只是要清楚 - 我已经搜索了互联网的深处并返回有关如何执行此操作的信息

I'm looking for assistance setting up pthread_Win32 to work with Visual Studio 2005. I'm programming in C, and I have a number of multithreaded assignments to write using pthread.h. However, since pthread is native to unix, I have to write all of my code, ftp it, and then ssh to my class' remote unix system to run it. It makes development take so much longer, and it's incredibly inefficient. I'd love (more than anything) to be able to get this working on my win32 machine, so I can develop in visual studio as I've been doing for quite some time.

我正在寻找设置 pthread_Win32 以与 Visual Studio 2005 一起使用的帮助。我使用 C 进行编程,并且我有许多使用 pthread.h 编写的多线程分配。但是,由于 pthread 是 unix 原生的,所以我必须编写我的所有代码,通过 ftp,然后 ssh 到我班级的远程 unix 系统来运行它。它使开发需要更长的时间,而且效率极低。我很想(最重要的是)能够在我的 win32 机器上运行它,这样我就可以在 Visual Studio 中进行开发,因为我已经做了很长一段时间了。

I've installed the pthread.lib file and pthread.h file into the respective lib/header directories, where all of the other files are. The DLL on the other hand (the actual library), I've placed in c:\windows\system32. I've tried to add the DLL as a dependency (right click project -> references -> Add new reference), but as others have stated, all I get is a blank dialogue box with no option to add any DLL files or anything. It seems to recognize the header file, but I get these errors when I compile:

我已将 pthread.lib 文件和 pthread.h 文件安装到相应的 lib/header 目录中,所有其他文件都在该目录中。另一方面,DLL(实际库),我放在 c:\windows\system32 中。我试图将 DLL 添加为依赖项(右键单击项目 -> 引用 -> 添加新引用),但正如其他人所说,我得到的只是一个空白对话框,没有添加任何 DLL 文件或任何内容的选项。它似乎可以识别头文件,但是在编译时出现这些错误:

1>Linking...

1>链接...

1>main.obj : error LNK2019: unresolved external symbol _imp_pthread_join referenced in function _main

1>main.obj : error LNK2019: unresolved external symbol _ imp_pthread_join 在函数 _main 中引用

1>main.obj : error LNK2019: unresolved external symbol _imp_pthread_create referenced in function _main

1>main.obj : error LNK2019: 未解析的外部符号 _ imp_pthread_create 在函数 _main 中引用

1>main.obj : error LNK2019: unresolved external symbol _imp_pthread_exit referenced in function _fcount

1>main.obj : error LNK2019: unresolved external symbol _ imp_pthread_exit 在函数 _fcount 中引用

From my research, I've determined that this is a problem with the DLL, and I'm assuming it can't find the definitions of the functions I've referenced in my code. I've searched high and low and I can't seem to figure out any way to overcome this problem. I've added the directories of the lib/header files to my linker, just in-case, but that didn't solve the issue. I need to do something, in visual studio, to specify that I need pthreadVC2.dll as a project dependency - and it seems to be impossible (and extremely frustrating) at this point.

根据我的研究,我确定这是 DLL 的问题,并且我假设它无法找到我在代码中引用的函数的定义。我搜索了高低,我似乎无法想出任何方法来克服这个问题。我已经将 lib/header 文件的目录添加到我的链接器中,以防万一,但这并没有解决问题。我需要在 Visual Studio 中做一些事情来指定我需要 pthreadVC2.dll 作为项目依赖项 - 在这一点上这似乎是不可能的(并且非常令人沮丧)。

Any words of wisdom?

有什么智慧的话吗?

Thank you very much

非常感谢

回答by Thib-o

I have been through this problem recently. It appears that the __imp__prefix is prepended to the function name in case pthread.his included in dynamic linking mode. Simply add the PTW32_STATIC_LIBdefine to your project or your source code before including pthread.hshould solve the issue.

我最近遇到了这个问题。如果pthread.h包含在动态链接模式中,则__imp__前缀似乎被添加到函数名称之前。在包含pthread.h之前,只需将PTW32_STATIC_LIB定义添加到您的项目或源代码中即可解决问题。

#define PTW32_STATIC_LIB
#include <pthread.h>

Although, I am not completely over as Visual Studio now trys to link with the _[FuncName]instead of [FuncName]

虽然,我还没有完全结束,因为 Visual Studio 现在尝试链接_[FuncName]而不是[FuncName]

In visual studio, function seems to be declared differently wether you are going to link them statically (.lib) or dynamically (.dll).

在 Visual Studio 中,函数的声明方式似乎有所不同,无论您是要静态 (.lib) 还是动态 (.dll) 链接它们。

To define a function you will link dynamically :

要定义一个函数,您将动态链接:

__declspec (dllimport) int myFunc(int myArgs) ;

To define function you are exporting for dynamic linking :

要定义为动态链接导出的函数:

__declspec (dllexport) int myFunc(int myArgs) ;

And the simpliest, to define a function you will link statically:

最简单的方法是定义一个静态链接的函数:

int myFunc(int myArgs) ;

[EDIT]

[编辑]

I am going on my investigations and went through thison MS help center. It seems that to avoid the _[FuncName] effect it is required to define a static linked library function by the following:

我正在进行我的调查,并在 MS 帮助中心完成了这项工作。似乎为了避免_[FuncName]效果,需要通过以下方式定义静态链接库函数:

int __cdecl myFunc(int MyArgs) ;

回答by Michael Burr

Have you added pthreadVC.lib(or whichever particular lib you need) to the project property:

您是否已将pthreadVC.lib(或您需要的任何特定库)添加到项目属性中:

Linker/Input/Additional Dependencies

It's not enough to just have the lib file in a particular directory, the linker needs to be told to use it as an input.

仅将 lib 文件放在特定目录中是不够的,需要告知链接器将其用作输入。

回答by bracelet

Just adding pthreadVC2.lib to linker list is not suffiecient. You also have to add addtional lib like pthreadVCE2.lib and pthreadVSE2.lib.

仅将 pthreadVC2.lib 添加到链接器列表是不够的。您还必须添加额外的库,如 pthreadVCE2.lib 和 pthreadVSE2.lib。

I am facing same issue but then I resolved it through adding these files.

我面临同样的问题,但后来我通过添加这些文件解决了它。