【问题标题】:How to properly configure meson / ninja / qt creator for recent c++ version?如何为最近的 c++ 版本正确配置 meson / ninja / qt creator?
【发布时间】:2021-04-21 21:09:36
【问题描述】:

我最近尝试运行一个使用智能指针的代码。正如标题所说,我使用 Ninja(版本 1.10.2)、Meson(0.57.2)和 Qt Creator 4.14.2。 我有以下 meson.build :

project('RayTracerDemo',
    'cpp',
    version: '0.0.0',
    default_options: [
        'c_std=c2x',
        'cpp_std=c++20',
        'warning_level=3',
        'optimization=3',
        'werror=true'
    ]
)

includeDir = include_directories([])

sources = files([
    'main.cpp'
])

headers = files([])

raytracer_exe = executable(
    'RayTracerDemo',
    sources: sources + headers,
    include_directories: includeDir,
    dependencies: []
)

但我仍然收到以下错误:

/~/programs/RayTracerDemo/main.cpp:189: error: ‘make_unique’ is not a member of ‘std’
../main.cpp: In function ‘void render(const std::vector<Sphere>&)’:
../main.cpp:189:52: error: ‘make_unique’ is not a member of ‘std’
  189 |             threads[height * width + width] = std::make_unique<std::thread>([&]() -> void
      |                                                    ^~~~~~~~~~~
../main.cpp:189:52: note: ‘std::make_unique’ is only available from C++14 onwards

对于以下几行:

threads[height * width + width] = std::make_unique<std::thread>([&]() -> void
{
    float xx = (2 * ((x + 0.5) * invWidth) - 1) * angle * aspectratio;
    float yy = (1 - 2 * ((y + 0.5) * invHeight)) * angle;
    Vec3f raydir(xx, yy, -1);
    raydir.normalize();
    *pixel = trace(Vec3f(0), raydir, spheres, 0);
});

线程是这样声明的向量:

std::vector<std::unique_ptr<std::thread>> threads(height * width);

Qt Creator 文档说使用 Meson here 不支持某些功能,但这不包括编译器版本问题。

【问题讨论】:

  • 你的编译版本是什么?如果你使用 cpp_std=c++14 怎么办?
  • 我有 g++ (GCC) 10.2.0,并且我已经尝试过 c++14 以上所有可能的版本,包括它。

标签: c++ qt smart-pointers ninja meson-build


【解决方案1】:

看起来介子选项一切正常,您只是忘记添加 标头(是的,错误消息有点混乱):

#include <memory>
...

另外,这可能是由于为 C++ 编译器设置了 c_std 选项,因为此选项适用于 C 编译器。

构建目录也可能没有(重新)正确配置。要检查当前配置的选项和标志,您可以使用:

$ meson configure <build dir>

然后重新配置:

$ meson setup --reconfigure <build dir>

顺便说一句(这与问题无关),这看起来很奇怪:

threads[height * width + width]  =

因为它与向量重叠,不应该吗? :

threads[invHeight * width + invWidth]  =

TL;DR

这个基本代码:

#include <iostream>
#include <memory>

int main()
{
    std::cout << "Hello World!" << std::endl;
    std::unique_ptr<int> a = std::make_unique<int>();
    return 0;
}

不会工作。编译器告诉我使用 make_unique 需要 C++ 14 或更高版本。问题是我已经在 meson.build 文件中放入了“project('ThreadPool', 'cpp', default_options : ['cpp_std=c++17'])”,但由于某种原因,这个文件没有被纳入Qt Creator 的帐户:检查构建设置是什么,选择的 C++ 版本是 C++11。

【讨论】:

  • 我已经包含了内存。如果我没有错,访问一维数组就好像它是一个二维数组是:array[y * x + x % y]?编辑:我完全错了。 Edit2:我的 for 循环很乱,谢谢你的注意,这确实不是我放的,而是threads[y * width + x]
  • @Bebel 好的,那么,你解决了构建错误吗?
  • 很遗憾,没有!我仍然得到同样的错误。我确定它来自 meson / ninja / qt creator,因为当我从命令行编译时它可以工作(我仍然得到一个不同的错误,但那是因为我的代码是错误的)。
  • @Bebel 然后您可以使用 $ meson introspect --targets build/ 检查所有编译器标志
  • @Babel 检查您是否(重新)正确配置了构建目录 $ meson configure
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
相关资源
最近更新 更多