【问题标题】:libstdc++ cannot find thread file on Maclibstdc++ 在 Mac 上找不到线程文件
【发布时间】:2018-03-29 10:17:23
【问题描述】:

请看一下这个简单的C++ 程序:

#include <iostream>
#include <thread>

void thread_test()
{
    std::cout << "Thread test\n";
}

int main(int argc, const char * argv[])
{
    std::thread t(thread_test);
    t.join();

    return 0;
}

我正在尝试在 macOS Sierra 上使用 Xcode 编译它。而且它不会编译。上面写着'thread' file not found。

在构建设置中,我将libstdc++ (GNU C++ standard library) 选项用于C++ Standard Library。如果我将此设置切换为libc++ (LLVM C++ standard library with C++11 support),那么没关系,它可以编译。但我的目标是通过使用libstdc++ 而不是libc++ 以某种方式编译它。甚至可能吗?请给我一点建议好吗?

【问题讨论】:

  • 你安装的 GCC 版本是什么?
  • 这是正确的行为。 std::thread 可从 C++11 获得
  • 您的确切编译器和链接器命令行是什么?以及编译器/链接器版本?
  • @JesperJuhl 配置为:--prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/ Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1 Apple LLVM 版本 8.1.0 (clang-802.0.42) 目标:x86_64-apple-darwin16.7.0 线程模型: posix InstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
  • @Jodocus 配置为:--prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Deve‌​ loper/Platforms/MacO‌​SX.platform/Develope‌​r/SDKs/MacOSX10.12.s‌​dk/usr/include/c++/4‌​.2.1 Apple LLVM 版本 8.1.0 (clang-802.0.42) 目标:x86_64 -apple-darwin16.7.0 线程模型:posix InstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeD‌​efault.xctoolchain/u‌​sr/bin –

标签: c++ xcode macos c++11 libstdc++


【解决方案1】:

Xcode 自带的 libstdc++ 版本很老,不包括thread。 macOS 上的 pthreads 缺少一些超时功能(例如 pthread_mutex_timedlock())。

您根本不能在 macOS 上同时使用 std::thread 和 Xcode 提供的 libstdc++。您必须切换到 libc++ 或使用不同的库 boost::thread 确实有效。 TinyThread++ 也是一个替代方案,可以在带有 libstdc++ 的 macOS 下工作。

【讨论】:

  • 完全可以在 MacOS 上安装和使用现代 C++14 工具链并使用std::thread。
  • 是的,但这远远超出了 Xcode 的支持范围。我已经更新了我的答案,以反映我说的是 Xcode 附带的 libstdc++ 4.2。 “我正在尝试在 macOS Sierra 上使用 Xcode 编译它。”将表明提问者想要使用 Xcode。
猜你喜欢
  • 2017-03-29
  • 2017-09-24
  • 2018-01-23
  • 1970-01-01
  • 2011-10-16
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
相关资源
最近更新 更多