c++ - 使用 boost spirit x3 分离解析器时出现链接错误

标签 c++ boost-spirit linker-errors boost-spirit-x3

我目前正在尝试使用 BOOST_SPIRIT_DEFINE/DECLARE/INSTANTIATE 将我的 boost spirit x3 解析器分成不同的 _def 和 .cpp 文件,但我一直收到链接错误。 HERE是我的解析器,它是分开的。

链接器错误读取

<artificial>:(.text.startup+0xbb): undefined reference to `bool kyle::parser::impl::parse_rule<__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::spirit::x3::context<boost::spirit::x3::skipper_tag, boost::spirit::x3::char_class<boost::spirit::char_encoding::standard, boost::spirit::x3::space_tag> const, boost::spirit::x3::unused_type>, boost::spirit::x3::unused_type const>(boost::spirit::x3::rule<kyle::parser::impl::identifier_class, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, false>, __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, boost::spirit::x3::context<boost::spirit::x3::skipper_tag, boost::spirit::x3::char_class<boost::spirit::char_encoding::standard, boost::spirit::x3::space_tag> const, boost::spirit::x3::unused_type> const&, boost::spirit::x3::unused_type const&)'

我做错了什么?我怎样才能使我的示例起作用?

  • 配置文件:

    #include <boost/spirit/home/x3.hpp>
    
    namespace kyle{
    namespace parser{
    
    namespace x3 = boost::spirit::x3;
    
    typedef std::string::const_iterator iterator_type;
    typedef x3::phrase_parse_context<x3::ascii::space_type>::type context_type;
    
    }
    }
    
  • 文字.cpp:

    #include "literals_def.hpp"
    #include "config.hpp"
    #include <boost/spirit/home/x3.hpp>
    
    
    namespace kyle {
    namespace parser {
    namespace impl {
    
    BOOST_SPIRIT_INSTANTIATE(identifier_type, iterator_type, context_type);
    
    }
    }
    }
    
  • 文字定义.hpp:

    #include <boost/spirit/home/x3.hpp>
    #include "literals.hpp"
    
    namespace kyle {
    namespace parser {
    namespace impl {
    
    namespace x3 = boost::spirit::x3;
    
    
    const identifier_type identifier = "identifier";
    
    
    
    auto const identifier_def = x3::alpha >> *x3::alnum;
    
    BOOST_SPIRIT_DEFINE(identifier)
    }
    impl::identifier_type identifier(){
        return impl::identifier;
    }
    
    
    }
    }
    
  • 文字.hpp:

    #include <boost/spirit/home/x3.hpp>
    
    namespace kyle{
    namespace parser{
    namespace impl {
    namespace x3 = boost::spirit::x3;
    
    struct identifier_class;
    
    typedef x3::rule<identifier_class, std::string> identifier_type;
    
    BOOST_SPIRIT_DECLARE(identifier_type)
    }
    
    impl::identifier_type identifier();
    
    
    }
    }
    
  • 主要.cpp:

    #include "literals.hpp"
    #include <iostream>
    
    template<typename Parser>
    bool test(std::string str, Parser&& p, bool full_match = true)
    {
        auto in = str.begin();
        auto end = str.end();
        bool ret = boost::spirit::x3::phrase_parse(in, end, p, boost::spirit::x3::space);
        ret &= (!full_match || (in == end));
        return ret;
    
    }
    
    int main(){
        auto b = test("fobar", kyle::parser::identifier());
        std::cout << b << std::endl;
    
    }
    

最佳答案

两点:

  1. 您将上下文定义为

    typedef x3::phrase_parse_context<x3::space_type>::type context_type;
    

    但是,您尝试使用 x3::space 调用它而不是 x3::ascii::space .

    错误消息中的提示是您没有包含:

    /home/sehe/custom/boost/boost/spirit/home/x3/nonterminal/rule.hpp:116: undefined reference to 'bool kyle::parser::impl::parse_rule<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::spirit::x3::context<boost::spirit::x3::skipper_tag, boost::spirit::x3::char_class<boost::spirit::char_encoding::standard, boost::spirit::x3::space_tag> const, boost::spirit::x3::unused_type>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(boost::spirit::x3::rule<kyle::parser::impl::identifier_class, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, false>, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, boost::spirit::x3::context<boost::spirit::x3::skipper_tag, boost::spirit::x3::char_class<boost::spirit::char_encoding::standard, boost::spirit::x3::space_tag> const, boost::spirit::x3::unused_type> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'

  2. 迭代器类型被推断为 std::string::iterator , 不是 std::string::const_iterator .修复它或不使用 auto总是:

Live On Melpon

完整代码

为了后代

  • 配置文件:

    #include <boost/spirit/home/x3.hpp>
    
    namespace kyle{
        namespace parser{
    
            namespace x3 = boost::spirit::x3;
    
            typedef std::string::const_iterator iterator_type;
            typedef x3::phrase_parse_context<x3::space_type>::type context_type;
    
        }
    }
    
  • 文字.cpp:

    #include "literals_def.hpp"
    #include "config.hpp"
    #include <boost/spirit/home/x3.hpp>
    
    namespace kyle { namespace parser { namespace impl {
        BOOST_SPIRIT_INSTANTIATE(identifier_type, iterator_type, context_type);
    } } }
    
  • 文字定义.hpp:

    #include <boost/spirit/home/x3.hpp>
    #include "literals.hpp"
    
    namespace kyle {
        namespace parser {
            namespace impl {
    
                namespace x3 = boost::spirit::x3;
    
                const identifier_type identifier = "identifier";
                auto const identifier_def = x3::alpha >> *x3::alnum;
    
                BOOST_SPIRIT_DEFINE(identifier)
            }
            impl::identifier_type identifier(){
                return impl::identifier;
            }
        }
    }
    
  • 文字.hpp:

    #include <boost/spirit/home/x3.hpp>
    
    namespace kyle{
        namespace parser{
            namespace impl {
                namespace x3 = boost::spirit::x3;
    
                struct identifier_class;
    
                typedef x3::rule<identifier_class, std::string> identifier_type;
    
                BOOST_SPIRIT_DECLARE(identifier_type)
            }
    
            impl::identifier_type identifier();
        }
    }
    
  • 主要.cpp:

    #include "literals.hpp"
    #include <iostream>
    
    template<typename Parser>
    bool test(std::string const& str, Parser p, std::string& output, bool full_match = true)
    {
        auto in = str.begin();
        auto end = str.end();
        bool ret = boost::spirit::x3::phrase_parse(in, end, p, boost::spirit::x3::space, output);
        ret &= (!full_match || (in == end));
        return ret;
    }
    
    int main(){
        std::string s;
        auto b = test("fobar", kyle::parser::identifier(), s);
        std::cout << b << ": " << s << std::endl;
    }
    
  • CMakeLists.txt:

    ADD_EXECUTABLE(sox3 main.cpp literals.cpp)
    
    SET(CMAKE_CXX_COMPILER g++-5)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem /home/sehe/custom/boost -std=c++14 -O3 -pthread -march=native -flto)
    

关于c++ - 使用 boost spirit x3 分离解析器时出现链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40496357/

相关文章:

c++ - 将对象作为参数传递给类构造函数

c++ - gSoap 在接口(interface)之间共享数据类型

c++ - 更改对话框的图标

c++ - 定义一个符号,它可能是 boost spirit 中文字函数的一部分

c++ - C 风格字符串的 boost::spirit::qi 和 qi::alnum 的模板编译错误

ios - Xcode 链接器问题

c++ - 如何传递作用域指针而不是 this 指针?

c++ - boost Spirit istream 迭代器给出误报

c++ - CMake Unresolved external 问题

c++ - 从 ASM(汇编 64 位)C++ 中获取值(value)