代码之家  ›  专栏  ›  技术社区  ›  Bruno Rijsman

“thrift-gen cpp”生成的代码不能在macOS上编译:'boost/tr1/功能.hpp'找不到文件'

  •  1
  • Bruno Rijsman  · 技术社区  · 7 年前

    我在macOS High Sierra 10.13.6上

    我安装节俭如下:

    $ brew install thrift
    

    $ thrift --version
    Thrift version 0.11.0
    

    我安装了boost,如下所示:

    $ brew install boost
    

    我有以下几点节俭文件:

    struct Person {
        1: required i32 age;
    }
    

    我运行Thrift编译器生成cpp代码,如下所示:

    $ thrift -gen cpp model.thrift
    

    #include "gen-cpp/model_types.h"
    
    int main(int argc, char const *argv[])
    {
        return 0;
    }
    

    我尝试编译文件如下:

    $ g++ -Wall -I /usr/local/include -L /usr/local/lib -o cpp_encode cpp_encode.cpp
    

    编译器版本如下:

     $ g++ --version
     Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
     Apple LLVM version 9.1.0 (clang-902.0.39.2)
     Target: x86_64-apple-darwin17.7.0
     Thread model: posix
     InstalledDir: /Library/Developer/CommandLineTools/usr/bin
    

    我得到以下编译错误:

     $ g++ -Wall -I /usr/local/include -L /usr/local/lib -o cpp_encode cpp_encode.cpp
     In file included from cpp_encode.cpp:1:
     In file included from ./gen-cpp/model_types.h:14:
     In file included from /usr/local/include/thrift/TBase.h:24:
     In file included from /usr/local/include/thrift/protocol/TProtocol.h:28:
     In file included from /usr/local/include/thrift/transport/TTransport.h:24:
     /usr/local/include/thrift/stdcxx.h:32:10: fatal error: 'boost/tr1/functional.hpp' file not found
     #include <boost/tr1/functional.hpp>
    

    目录/usr/local/boost存在:

    $ ls -1 /usr/local/include/boost
    accumulators
    algorithm
    align
    align.hpp
    aligned_storage.hpp
    any.hpp
    archive
    [...]
    

    但目录/usr/local/include/boost/tr1不存在:

    $ ls -1 /usr/local/include/boost/tr1
    ls: /usr/local/include/boost/tr1: No such file or directory
    

    我应该安装/usr/local/include/boost/tr1吗?如果是,怎么做?

    我应该不一样地使用节俭吗?

    3 回复  |  直到 7 年前
        1
  •  3
  •   Corristo    7 年前

    您可以查看导致包含 boost/tr1/functional 在里面 thrift/stdcxx.h

    #if defined(BOOST_NO_CXX11_HDR_FUNCTIONAL) || (defined(_MSC_VER) \
        && _MSC_VER < 1800) || defined(FORCE_BOOST_FUNCTIONAL)
    #include <boost/tr1/functional.hpp>
    #define _THRIFT_FUNCTIONAL_TR1_ 1
    #endif
    

    因为我们可以放心地假设您的MacOS g++没有定义 _MSC_VER 剩下的两个选项只有一个:要么你自己定义 FORCE_BOOST_FUNCTIONAL 宏(不太可能),或 BOOST_NO_CXX11_HDR_FUNCTIONAL 未定义。

    通过看 boost config documentation 增强\u否\u CXX11 \u HDR \u功能正常

    <functional> .

    若要测试CLAN版本是否具有默认启用的C++ 11,可以尝试编译以下内容的文件

    int main() 
    {
      return []() -> int { return 0; }
    }
    

    不设置任何编译器标志。

    • 检查您(或您正在使用的除节俭外的第三方库)是否正在定义 强制增强功能
    • 如果没有添加,请检查编译器是否默认支持C++ 11 -std=c++11 到编译器标志列表
    • 如果不是,尝试升级你的Boost版本,也许一个更新版本会正确地识别你的编译器支持C++ 11的功能。
    • 否则,用boost.config文件.
        2
  •  3
  •   Bruno Rijsman    7 年前

    这里有几个问题:

    如果您想让程序不仅编译而且链接,还有一些附加问题:

    bool Coordinate::operator<(const Coordinate& other) const
    {
        if (x < other.x) {
            return true;
        } else if (x > other.x) {
            return false;
        } else if (y < other.y) {
            return true;
        } else {
            return false;
        }
    }
    

    鉴于上述所有情况,以下工作:

    clang++ -std=c++11 -stdlib=libc++ -Wall -o cpp_encode cpp_encode.cpp gen-cpp/model_types.cpp
    

    注1:这个简单的程序不需要它,但是一旦你编写了更完整的节俭代码,你就需要链接到节俭库(-lthrift)和boost库(-lboost)。

        3
  •  0
  •   sehe    7 年前

    看来thrift正在生成不推荐使用的代码。

    也许你可以升级到一个新版本,如果它存在的话。

    这个 TR1 bits

    所以一般来说,相同的东西也存在,但不再使用tr1名称空间。例如。

    #include <boost/function.hpp>
    

    可能有用,或者

    #include <functional>