如何通过 Eclipse 向 g++ 添加“-pthread”选项?

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

How to add the '-pthread' option to g++ via Eclipse?

eclipsegccg++

提问by Zippo

I am compiling a multithreaded library using eclipse.

我正在使用 eclipse 编译一个多线程库。

How can i add the -pthread or -pthreads option to the call made to g++ by eclipse?

如何将 -pthread 或 -pthreads 选项添加到 eclipse 对 g++ 的调用中?

回答by Ali

The original answer that I wrote almost 2 years ago is now outdated; here is how to do it today.

我大约两年前写的原始答案现在已经过时了;这是今天的方法。

Either right click on the project folder or File > Properties, then C/C++ Build > Settings. At the GCC C++ Linker > Linker flagsadd -pthreadand finally hit the Applybutton. That's all.

右键单击项目文件夹或File > Properties,然后C/C++ Build > Settings。在GCC C++ Linker > Linker flags添加-pthread并最后点击Apply按钮。就这样。

I have tested this with gcc 4.8 and Eclipse Luna on Ubuntu 14.04. Thank to Neil Traft for pointing it out in a comment.

我已经在 Ubuntu 14.04 上使用 gcc 4.8 和 Eclipse Luna 对此进行了测试。感谢 Neil Traf 在评论中指出。





The original answer from February 2014, now outdated:

2014 年 2 月的原始答案现已过时:

Either right click on the project folder or File > Properties, then C/C++ Build > Settings.

右键单击项目文件夹或File > Properties,然后C/C++ Build > Settings

At the GCC C++ Compiler > Miscellaneousadd -pthreadto Other flagsand hit the Applybutton.

GCC C++ Compiler > Miscellaneous添加-pthread其他标志并点击应用按钮。

At the GCC C++ Linker > Librariesclick the icon with the green plus sign on the Librariesbar to add pthreadand finally hit the Applybutton.

GCC C++ Linker > Libraries单击Libraries栏上带有绿色加号的图标进行添加pthread,最后点击Apply按钮。

This must solve it.

这必须解决它。

Note that you have to tell both the compiler and the linker that you are compiling a multi-threaded application (passing -pthreadto the compiler and -lpthreadto the linker).

请注意,您必须告诉编译器和链接器您正在编译多线程应用程序(传递-pthread给编译器和-lpthread链接器)。