c++ - 如果有 std::thread,Visual Studio 2017 linux 无法编译
问题描述
所以我正在尝试使用 Visual Studio 2017 创建一个 Linux 项目。我从一个空的 Linux 项目(作为项目模板)开始,到目前为止一切都很好。
但是,如果我有
// Create a new thread for the connection to avoid clutter
std::thread newConnectionHandler(connectionHandler, iNewConnection);
newConnectionHandler.detach();
在我的代码中,它不会编译。这些是我得到的错误:
Error E0020 identifier "__float128" is undefined
Error In function `std::thread::thread<void(&)(int), int&>(void(&)(int), int&)':
Error undefined reference to `pthread_create'
Error ld returned 1 exit status
但是只要我注释掉这些std::thread
东西,它就会编译。
这是我到目前为止所尝试的:
- C/C++ -> 附加选项 -> 添加
-pthread -lpthread
或只是-pthread
- 链接器 -> 附加选项 -> 添加
-pthread -lpthread
或只是-pthread
- 链接器 -> 库依赖项 -> 添加
thread
- 在 C++17 和 C++11 之间切换 C++ 语言标准
当然,我已经尝试了这些更改的多种变体,但我总是遇到同样的错误。
我的包括:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <thread>
编辑:这也是声明connectionHandler
:
void connectionHandler(int iConnection)
传递时iNewConnection
,数据类型为int
.
解决方案
将-pthread
选项设置为Linker -> Command Line -> Additional Options
,它也应该是链接命令行中所有对象和库文件之后的最后一个。
顺序很重要。
推荐阅读
- javascript - Notepad++ 不会执行 if else 语句中的条件,即使它执行了我的提示功能?
- javascript - JS 通过嵌套 json 创建自定义 json
- angular - Angular Material - 在组件级别覆盖覆盖 css
- go - 如何在 Go 1.11+ for Golang 中使用 GOPATH 之外的整体 repo 架构共享本地模块
- java - 使用 Map 的方法 merge()
将字符串附加到 StringBuilder(value) - linux - 无法为 python 链接共享库
- vue.js - RouterLink 在 Vue Js 中未正确显示
- rxjs - 用大理石图测试去抖动
- angularjs - 在 angular.js 中包含带有 $provide.factory 的工厂函数后无法调用工厂方法
- javascript - 将参数传递给 Function Express JS