【问题标题】:Visual Studio 2017 linux can't compile if there's std::thread如果有 std::thread,Visual Studio 2017 linux 无法编译
【发布时间】:2019-04-04 10:01:47
【问题描述】:

所以我正在尝试使用 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

【问题讨论】:

  • @chris 好的,完成。
  • 您在摆弄链接器,但您的错误是编译器错误。我认为你的标题路径是错误的。
  • @Quentin,也许我应该保留这一半的原始评论,但第一个错误是 Intellisense,其余的是链接器输出,这意味着编译器可以正常处理。
  • @chris 哦,好吧。那我没有更好的建议了:)
  • @Kordnak:-pthread 是链接命令行中所有对象和库文件之后的最后一个吗?

标签: c++ linux multithreading visual-studio compiler-errors


【解决方案1】:

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

顺序很重要。

【讨论】:

  • “还不够”是什么意思?我刚刚使用一个新的 Linux 空项目进行了测试,只需将 -pthread 添加到链接器的附加选项中即可。
  • 您在评论中说,这就是解决您的问题的方法,对吧?如果无论其位置如何都有效,请确认,我将删除此答案。
【解决方案2】:

将 -pthread 放入链接器 -> 命令行 -> 附加选项有效。即使您使用的是 linux 项目,您也需要在 Visual Studio 中选择项目属性。

谢谢!!

【讨论】:

  • 嗨,arimaknp,欢迎来到 StackOverflow。如果另一个答案为您解决了问题,请考虑对该答案进行投票,而不是添加重复的答案。这将帮助其他用户快速找到答案并减少混乱
猜你喜欢
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 2017-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多