首页 > 解决方案 > 如果有 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东西,它就会编译。

这是我到目前为止所尝试的:

当然,我已经尝试了这些更改的多种变体,但我总是遇到同样的错误。

我的包括:

#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.

标签: c++linuxmultithreadingvisual-studiocompiler-errors

解决方案


-pthread选项设置为Linker -> Command Line -> Additional Options,它也应该是链接命令行中所有对象和库文件之后的最后一个。

顺序很重要。


推荐阅读