C++库的兼容性问题

3

我正在尝试使用CMake生成一个iOS Xcode项目。首先,我生成了该项目,并且没有出现任何错误。一旦生成完成,我尝试使用下面的命令进行构建:

cmake --build _builds --config Release --target install

这时我就会遇到错误。我想可能是因为我使用的库的C++版本与编译器不兼容引起的;但我不确定。这是运行构建命令时出现的错误:

/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/utils.hh:77:14: error: expression is not an integral
      constant expression
        case Code::NONE:
             ^~~~~~~~~~
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:42:1: error: unknown type name
      'constexpr'
constexpr  auto         ab_generators = std::array<unsigned,12>
^
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:42:25: error: illegal storage class on
      file-scoped variable
constexpr  auto         ab_generators = std::array<unsigned,12>
                        ^
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:43:3: error: expected '(' for
      function-style cast or type construction
  {
  ^
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:48:1: error: unknown type name
      'constexpr'
constexpr  unsigned int ab_rate       = ab_generators.size();
^
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:49:1: error: unknown type name
      'constexpr'
constexpr  unsigned int order         = 15;
^
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:62:1: error: unknown type name
      'constexpr'
constexpr  unsigned int state_count = (1 << order);
^
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:63:1: error: unknown type name
      'constexpr'
constexpr  unsigned int state_mask  = (1 << order) - 1;
^
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:70:12: error: expression is not an
      integral constant expression
      case ConvBlockType::a:
           ^~~~~~~~~~~~~~~~
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:71:12: error: expression is not an
      integral constant expression
      case ConvBlockType::b:  return (msg_size + order) * ab_rate / 2;
           ^~~~~~~~~~~~~~~~
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:72:12: error: expression is not an
      integral constant expression
      case ConvBlockType::ab: return (msg_size + order) * ab_rate;
           ^~~~~~~~~~~~~~~~~
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:143:27: error: a space is required
      between consecutive right angle brackets (use '> >')
  vector<vector<StateEntry>> error_count;
                          ^~
                          > >
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:143:17: warning: template argument uses
      local type 'StateEntry' [-Wlocal-type-template-args]
  vector<vector<StateEntry>> error_count;
                ^~~~~~~~~~
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:145:55: error: expected '(' for
      function-style cast or type construction
    error_count.emplace_back (state_count, StateEntry {0, -1, 0});
                                           ~~~~~~~~~~ ^
/Users/edwardpizzurro/Desktop/watermark-lib/audiowmark/convcode.cc:145:17: error: no member named
      'emplace_back' in 'std::__1::vector<std::__1::vector<StateEntry, std::__1::allocator<StateEntry> >,
      std::__1::allocator<std::__1::vector<StateEntry, std::__1::allocator<StateEntry> > > >'; did you mean
      '__emplace_back'?
    error_count.emplace_back (state_count, StateEntry {0, -1, 0});

这是我运行后得到的结果:

g++ --version

结果:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

1
代码至少是用C++11编写的。你正在使用那个版本进行编译吗? - cigien
我不确定如何检查我的CMake正在使用哪个版本进行编译。 - Edward Pizzurro
1
检查版本。 - Abdullah Sheikh
指定 C++ 的版本,不是编译器。 - 김선달
2个回答

0

检查 g++ 的版本。

g++ --version

你的代码是在 >= c++11 版本下运行的。


我更新了问题,并附上了运行命令的结果。我不确定它对我说了什么。 - Edward Pizzurro
OP的问题是询问如何设置编译器选项为C++11,而不是如何检查正在使用的版本。最好在问题的跟进评论中提出这个问题。 - cigien

0
如果您可以访问CMakeLists.txt文件,请添加:
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

这样可以确保所有的子项目也以C++17进行编译。如果因为某些原因无法更改CMakeLists.txt文件,您可以在配置时通过命令行传递编译器标志:

cmake -S . -B _builds -DCMAKE_CXX_FLAGS="-std=c++17"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接