如何在 Visual Studio Community Edition 中将 pthread 库添加到 C++ 项目? [英] How to add pthread library to C++ project in Visual Studio Community Edition?

查看:41
本文介绍了如何在 Visual Studio Community Edition 中将 pthread 库添加到 C++ 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

但是,当检测到 .h 文件时,我收到一个错误:

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

Error   1   error LNK2019: unresolved external symbol __imp__pthread_create 

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

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

我以为我可以将 .lib 文件添加到 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:

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 

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

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.

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

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

推荐答案

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

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

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

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.

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

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

这是他的教程:

将 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 文件夹.

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.

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

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.

这篇关于如何在 Visual Studio Community Edition 中将 pthread 库添加到 C++ 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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