如何在 Visual Studio Community Edition 中将 pthread 库添加到 C++ 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28975700/
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-08-28 21:07:47  来源:igfitidea点击:

How to add pthread library to C++ project in Visual Studio Community Edition?

c++visual-studiolibraries

提问by James Joshua Street

I wanted to be able to use pthreads in visual studio because I was using it to debug, and was following the tutorial here.

我希望能够在 Visual Studio 中使用 pthreads,因为我正在使用它进行调试,并且正在按照此处的教程进行操作。

http://web.cs.du.edu/~sturtevant/pthread.html

http://web.cs.du.edu/~sturtevant/pthread.html

It seems simple enough, add the .h files to the C++ include directory and add the .lib file to the lib directory.

看起来很简单,将.h文件添加到C++的include目录,将.lib文件添加到lib目录。

However, while the .h files are being detected, I am getting an error:

但是,在检测到 .h 文件时,出现错误:

Error   1   error LNK2019: unresolved external symbol __imp__pthread_create 

I take this to mean that the .lib file is not being detected properly.

我认为这意味着 .lib 文件没有被正确检测到。

I thought I could just add the .lib file to the lib listing at Project Properties -> Linker -> Additional Dependencies using its full path like so:

我想我可以将 .lib 文件添加到项目属性 -> 链接器 -> 附加依赖项的 lib 列表中,使用其完整路径,如下所示:

D:\Visual Studio\VC\lib\pthreadVC2.lib;kernel32.lib;user32.lib;

Not sure if there is any advantage to appending the new lib to the beginning of the list or the end of the list, but neither seems to work.

不确定将新库附加到列表的开头或结尾是否有任何优势,但似乎都不起作用。

In the command line tab, we can see the command that is actually run (I think):

在命令行选项卡中,我们可以看到实际运行的命令(我认为):

/OUT:"C:\projects\GTKWavePipe\NamedPipeTest\Debug\NamedPipeTest.exe" /MANIFEST /NXCOMPAT /PDB:"C:\projects\GTKWavePipe\NamedPipeTest\Debug\NamedPipeTest.pdb" /DYNAMICBASE "D:\Visual Studio\VC\lib\pthreadVC2.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X86 /INCREMENTAL /PGD:"C:\projects\GTKWavePipe\NamedPipeTest\Debug\NamedPipeTest.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"Debug\NamedPipeTest.exe.intermediate.manifest" /ERRORREPORT:PROMPT /NOLOGO /TLBID:1 

Does anyone know what I might be doing wrong? Ultimately I can always just go do my debugging in my cygwin environment, but the debugger I'm using there is not as good.

有谁知道我可能做错了什么?最终,我总是可以在我的 cygwin 环境中进行调试,但是我在那里使用的调试器并没有那么好。

Plus, I think being able to add libraries into visual studio seems like a useful skill.

另外,我认为能够将库添加到 Visual Studio 似乎是一项有用的技能。

回答by mgarey

Did you forget to add the .dll file to the bin folder?

您是否忘记将 .dll 文件添加到 bin 文件夹中?

If you follow his tutorial exactly, you won't need to add any .lib files in Additional Dependencies (note steps 6-10 in his tutorial). I followed his tutorial and got it to work. I'm using Visual Studio 12.0 Ultimate, but it should be the same or a very similar process for your version of Visual Studio.

如果您完全按照他的教程进行操作,则不需要在其他依赖项中添加任何 .lib 文件(注意他教程中的步骤 6-10)。我跟着他的教程,让它工作。我使用的是 Visual Studio 12.0 Ultimate,但它应该与您的 Visual Studio 版本相同或非常相似。

I actually followed his tutorial here: http://web.cs.du.edu/~sturtevant/w13-sys/InstallingpthreadsforVisualStudio.pdfwhere he provides you a compressed folder containing the files you need. I got the files from there.

我实际上在这里遵循了他的教程:http: //web.cs.du.edu/~sturtevant/w13-sys/InstallingpthreadsforVisualStudio.pdf在那里他为您提供了一个包含您需要的文件的压缩文件夹。我从那里得到了文件。

Here's his tutorial again:

这是他的教程:

Add the 3 .h files (pthread.h, sched.h, sempahore.h) to the include folder under \VC (mine is C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC). Add the pthreadVC2.lib to the lib folder and pthreadVC2.dll file to the bin folder.

Next, open the property manager: in Visual Studio, go to View>Other Windows>Property Manager. Expand the Debug folder. Open Microsoft.Cpp.Win32.user (or a similarly named Property Sheet). Go to Common Properties>Linker>Input. In Additional Dependencies add pthreadVC2.lib as a dependency.

将 3 个 .h 文件(pthread.h、sched.h、sempahore.h)添加到 \VC 下的包含文件夹(我的是 C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC)。将 pthreadVC2.lib 添加到 lib 文件夹,将 pthreadVC2.dll 文件添加到 bin 文件夹。

接下来,打开属性管理器:在 Visual Studio 中,转到“查看”>“其他 Windows”>“属性管理器”。展开调试文件夹。打开 Microsoft.Cpp.Win32.user(或类似名称的属性表)。转到通用属性>链接器>输入。在附加依赖项中添加 pthreadVC2.lib 作为依赖项。

回答by Kremi Jowo

I guess your problem is similar with mine.

我想你的问题和我的相似。

Although you are using windows 64bit, don't copy 64bitof dll and lib file. Choose 86 instead.

尽管您使用的是 windows 64bit,但不要复制 64bit的 dll 和 lib 文件。 改为选择 86

Mine worked that way.

我的就是这样工作的。