g++ thread error:unrecognized command line option '-std=C++11'

本文介绍了在Windows 10系统使用Cygwin环境下编译C++多线程程序遇到的问题及解决过程。从错误的编译指令出发,探讨了正确的线程编译方式,并给出了在g++版本较低时的解决方案,最终转向Linux进行编译。

系统:win10

编译环境:g++ 4.6

内容:pthread

在windows系统中通过cygwin编译c++ 多线程程序,编译指令: g++ Thread.cpp -static-libgcc -static-libstdc++      


发现报错,百度查询说是线程编译指令是:g++ -Wl,--no-as-needed -std=C++11 -pthread Thread.cpp

运行命令如下:

提示错误:无法识别的选项'-pthread'    想起来百度是看到的应该加一个 ' l ',所以命令改为:

g++ -Wl,--no-as-needed -std=C++11 -lpthread Thread.cpp  运行结果如下:


百度说是g++版本过低的问题,通过g++  --version 查看了自己的g++版本是4.6,看百度上是说要升级到4.8才可以。

嫌麻烦,不想升级,直接在linux编译。

linux g++ 版本:4.8

通过命令:g++ -std=c++11 -pthread Thread.cpp  



user@YBXzuiniubi:~/Livox-SDK/build$ cmake .. make sudo make install -- main project dir: /home/user/Livox-SDK -- Configuring done -- Generating done -- Build files have been written to: /home/user/Livox-SDK/build Consolidate compiler generated dependencies of target livox_sdk_static [ 2%] Building CXX object sdk_core/CMakeFiles/livox_sdk_static.dir/src/base/thread_base.cpp.o In file included from /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:25: /home/user/Livox-SDK/sdk_core/src/base/thread_base.h:46:8: error: ‘shared_ptr’ in namespace ‘std’ does not name a template type 46 | std::shared_ptr<std::thread> thread_; | ^~~~~~~~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.h:30:1: note:std::shared_ptr’ is defined in header ‘<memory>’; did you forget to ‘#include <memory>’? 29 | #include "noncopyable.h" +++ |+#include <memory> 30 | /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp: In member function ‘virtual bool livox::ThreadBase::Start()’: /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:3: error:thread_’ was not declared in this scope; did you mean ‘pthread_t’? 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~~~~ | pthread_t /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:18: error: ‘make_shared’ is not a member of ‘std’ 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~~~~~~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:26:1: note:std::make_shared’ is defined in header ‘<memory>’; did you forget to ‘#include <memory>’? 25 | #include "thread_base.h" +++ |+#include <memory> 26 | #include <thread> /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:41: error: expected primary-expression before ‘>’ token 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:68: error: left operand of comma operator has no effect [-Werror=unused-value] 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp: In member function ‘virtual void livox::ThreadBase::Join()’: /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:41:5: error:thread_’ was not declared in this scope; did you mean ‘pthread_t’? 41 | thread_->join(); | ^~~~~~~ | pthread_t At global scope: cc1plus: note: unrecognized command-line option-Wno-c++11-long-long’ may have been intended to silence earlier diagnostics cc1plus: all warnings being treated as errors make[2]: *** [sdk_core/CMakeFiles/livox_sdk_static.dir/build.make:104: sdk_core/CMakeFiles/livox_sdk_static.dir/src/base/thread_base.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:242: sdk_core/CMakeFiles/livox_sdk_static.dir/all] Error 2 make: *** [Makefile:136: all] Error 2 Consolidate compiler generated dependencies of target livox_sdk_static [ 2%] Building CXX object sdk_core/CMakeFiles/livox_sdk_static.dir/src/base/thread_base.cpp.o In file included from /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:25: /home/user/Livox-SDK/sdk_core/src/base/thread_base.h:46:8: error: ‘shared_ptr’ in namespace ‘std’ does not name a template type 46 | std::shared_ptr<std::thread> thread_; | ^~~~~~~~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.h:30:1: note:std::shared_ptr’ is defined in header ‘<memory>’; did you forget to ‘#include <memory>’? 29 | #include "noncopyable.h" +++ |+#include <memory> 30 | /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp: In member function ‘virtual bool livox::ThreadBase::Start()’: /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:3: error:thread_’ was not declared in this scope; did you mean ‘pthread_t’? 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~~~~ | pthread_t /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:18: error: ‘make_shared’ is not a member of ‘std’ 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~~~~~~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:26:1: note:std::make_shared’ is defined in header ‘<memory>’; did you forget to ‘#include <memory>’? 25 | #include "thread_base.h" +++ |+#include <memory> 26 | #include <thread> /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:41: error: expected primary-expression before ‘>’ token 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:34:68: error: left operand of comma operator has no effect [-Werror=unused-value] 34 | thread_ = std::make_shared<std::thread>(&ThreadBase::ThreadFunc, this); | ^~~~ /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp: In member function ‘virtual void livox::ThreadBase::Join()’: /home/user/Livox-SDK/sdk_core/src/base/thread_base.cpp:41:5: error:thread_’ was not declared in this scope; did you mean ‘pthread_t’? 41 | thread_->join(); | ^~~~~~~ | pthread_t At global scope: cc1plus: note: unrecognized command-line option-Wno-c++11-long-long’ may have been intended to silence earlier diagnostics cc1plus: all warnings being treated as errors make[2]: *** [sdk_core/CMakeFiles/livox_sdk_static.dir/build.make:104: sdk_core/CMakeFiles/livox_sdk_static.dir/src/base/thread_base.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:242: sdk_core/CMakeFiles/livox_sdk_static.dir/all] Error 2 make: *** [Makefile:136: all] Error 2 user@YBXzuiniubi:~/Livox-SDK/build$
09-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值