使用boost spirit x3在单独的解析器中链接错误

Exa*_*gon 3 c++ linker-errors boost-spirit boost-spirit-x3

我是currentyl试图将我的提升精神x3解析器分成不同的_def和.cpp文件使用BOOST_SPIRIT_DEFINE/DECLARE/INSTANTIATE,但我不断收到链接错误. 这里是我的解析器分离.

链接器错误读取

<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&)'
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我怎样才能让我的例子有效?

  • config.hpp:

    #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;
    
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • literals.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);
    
    }
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • literals_def.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;
    }
    
    
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • literals.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();
    
    
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • main.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;
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

seh*_*ehe 6

两点:

  1. 您将上下文定义为

    typedef x3::phrase_parse_context<x3::space_type>::type context_type;
    
    Run Code Online (Sandbox Code Playgroud)

    但是,您尝试使用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.修复它或不使用autoalways:

Live On Melpon

完整的代码

对于后代

  • config.hpp:

    #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;
    
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • literals.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);
    } } }
    
    Run Code Online (Sandbox Code Playgroud)
  • literals_def.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;
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • literals.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();
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • main.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;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 的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)
    
    Run Code Online (Sandbox Code Playgroud)

  • 仅供参考:当我仍然在https://www.livecoding.tv/sehe/videos/上播放我的SO回答时,您可以欣赏我找到这个答案需要多长时间.(暗示它不快) (2认同)

Joe*_*man 5

除了sehe的回答,让我补充一点:

您需要对声明的内容和实际使用的内容非常准确。X3 允许任何类型的可能类型,但链接器不允许。

这是一个提示:当出现链接器错误时,在 BOOST_SPIRIT_INSTANTIATE 之后,声明如下内容:

int x = context_type{};
Run Code Online (Sandbox Code Playgroud)

这将是一个错误,是的,因为context_type 无法转换为int。但是该错误也会为您提供上下文的确切类型。将其与链接器错误进行比较,您将看到您的错误。