【问题标题】:"error: variable length array of non-POD element type" when compiling re::engine::RE2编译 re::engine::RE2 时出现“错误:非 POD 元素类型的可变长度数组”
【发布时间】:2013-06-30 16:18:27
【问题描述】:

我正在尝试使用cpan 安装re::engine::RE2 模块。

编译时,编译器会发出错误信息:

re2_xs.cc:254:25: error: variable length array of non-POD element type
      're2::StringPiece'
    re2::StringPiece res[re->nparens + 1];
                        ^

这是什么意思?我该如何解决?我使用的是 2011 Macbook Air 和 Perl 5.12。

【问题讨论】:

  • 你安装了 GCC 吗?
  • 尝试使用cpancpanm安装它。
  • 嗯,我就是这么做的?
  • 对不起,我错过了,尝试下载模块并手动安装。
  • 也这样做了。同样的错误信息。

标签: perl compiler-errors cpan re2


【解决方案1】:

re::engine::RE2 was never successfully tested on darwin/Perl 5.12.

在谷歌上搜索错误消息表明这是 Xcode 附带的旧 clang 中的一个缺陷,并且不存在于较新版本中。

升级你的软件,you're out of support,也许使用 Macports 来获得一个新的 gcc。

【讨论】:

    【解决方案2】:

    没错,re::engine::RE2 从未在 OS X 上编译过。但你可以让它编译。找到 cpan 目录。对我来说是~/.cpan/build/re-engine-RE2-0.13-BY20k3/。然后在其中更改两个 C++ 源文件。

    $ diff -p re2_xs.cc.old re2_xs.cc
    *** re2_xs.cc.old   2015-04-20 20:20:15.000000000 +0200
    --- re2_xs.cc   2015-04-20 20:22:24.000000000 +0200
    *************** RE2_exec(pTHX_ REGEXP * const rx, char *
    *** 229,236 ****
          RE2 * ri = (RE2*) SvANY(rx)->pprivate;
          regexp * re = SvANY(rx);
    
    -     re2::StringPiece res[re->nparens + 1];
    -
      #ifdef RE2_DEBUG
          Perl_warner(aTHX_ packWARN(WARN_MISC), "RE2: Matching '%s' (%p, %p) against '%s'", stringarg, strbeg, stringarg, RX_WRAPPED(rx));
      #endif
    --- 229,234 ----
    *************** RE2_exec(pTHX_ REGEXP * const rx, char *
    *** 241,246 ****
    --- 239,246 ----
            return 0;
          }
    
    +     re2::StringPiece *res = new re2::StringPiece[re->nparens + 1];
    +
          bool ok = ri->Match(
                  re2::StringPiece(strbeg, strend - strbeg),
                  stringarg - strbeg,
    *************** RE2_exec(pTHX_ REGEXP * const rx, char *
    *** 250,255 ****
    --- 250,256 ----
    
          /* Matching failed */
          if (!ok) {
    +         delete [] res;
              return 0;
          }
    
    *************** RE2_exec(pTHX_ REGEXP * const rx, char *
    *** 266,271 ****
    --- 267,274 ----
              }
          }
    
    +     delete [] res;
    +
          return 1;
      }
    

    在此之后尝试编译会让您遇到tr1 问题。

    /usr/bin/clang -o obj/util/arena.o -xc++   -O3 -DHAVE_PTHREAD -pthread -Wno-sign-compare -c -I.    -DNDEBUG util/arena.cc
    In file included from util/arena.cc:5:
    ./util/util.h:45:10: fatal error: 'tr1/unordered_set' file not found
    

    我确信对 Makefile 有更简单的 -D 修复,但我更改了代码。

    # diff -p re2/util/util.h.old re2/util/util.h
    *** re2/util/util.h.old 2015-04-20 20:29:01.000000000 +0200
    --- re2/util/util.h 2015-04-20 20:29:26.000000000 +0200
    *************** using std::make_pair;
    *** 42,49 ****
    
      #if defined(__GNUC__) && !defined(USE_CXX0X)
    
    ! #include <tr1/unordered_set>
    ! using std::tr1::unordered_set;
    
      #else
    
    --- 42,51 ----
    
      #if defined(__GNUC__) && !defined(USE_CXX0X)
    
    ! //#include <tr1/unordered_set>
    ! //using std::tr1::unordered_set;
    ! #include <unordered_set>
    ! using std::unordered_set;
    
      #else
    

    现在就

    make && make install
    

    您现在可以在 OS X 上使用 re::engine::RE2

    【讨论】:

      【解决方案3】:

      Douglas Scofield 的答案是正确的,但需要进行额外更改。在 re2_xs.cc 中,您还必须将第 244 行更改如下:

                bool ok = ri->Match(
                    re2::StringPiece(strbeg, strend - strbeg),
                    stringarg - strbeg,
                    strend - strbeg,
                    RE2::UNANCHORED,
      !             res, re->nparens + 1);
      

      Douglas Scofield 的回答将数组 res 更改为指针 *res 以避免变长数组的编译器错误。但是,在此之后更改行

      sizeof res / sizeof *res
      

      不起作用,因为您将获取指针的大小并除以数组的大小。因此,您必须将上述更改为

      re->nparens + 1
      

      获取 *res 指向的数组的大小,这是原始代码中所需要的。

      如果您不进行更改,则捕获组将无法在 re2 中工作。您将收到错误,您的捕获组(如 $1、$2 等)未初始化。

      上面的答案太好了,只需要一个附录。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-26
        相关资源
        最近更新 更多