如何运行Wave快速入门示例?

How can I run Wave quick start example?(如何运行Wave快速入门示例?)
本文介绍了如何运行Wave快速入门示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(源自this问题。)

我尝试使用this示例失败。

我的尝试是复制并稍微编辑链接处的代码。结果是这样(我留下Boost网站的评论,以便更容易将我的编辑与原始版本进行比较):

#include <boost/wave.hpp>
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>

int main() {
    // The following preprocesses a given input file.
    // Open the file and read it into a string variable
    std::string input("int main() { std::cout << "Hello world"; }");
    // The template boost::wave::cpplexer::lex_token<> is the  
    // token type to be used by the Wave library.
    // This token type is one of the central types throughout 
    // the library, because it is a template parameter to some 
    // of the public classes and templates and it is returned 
    // from the iterators.
    // The template boost::wave::cpplexer::lex_iterator<> is
    // the lexer iterator to use as the token source for the
    // preprocessing engine. In this case this is parameterized
    // with the token type.
    typedef boost::wave::cpplexer::lex_iterator<
            boost::wave::cpplexer::lex_token<> >
        lex_iterator_type;
    typedef boost::wave::context<
            std::string::iterator, lex_iterator_type>
        context_type;

    context_type ctx(input.begin(), input.end(), "preproc.cpp");

    // At this point you may want to set the parameters of the
    // preprocessing as include paths and/or predefined macros.
    //ctx.add_include_path("...");
    //ctx.add_macro_definition("...");

    // Get the preprocessor iterators and use them to generate 
    // the token sequence.
    context_type::iterator_type first = ctx.begin();
    context_type::iterator_type last = ctx.end();

    // The input stream is preprocessed for you during iteration
    // over [first, last)
    while (first != last) {
        std::cout << (*first).get_value();
        ++first;
    }
}

我可以使用

成功编译
g++ -std=c++17 -lboost_thread -lboost_filesystem -lboost_wave source.cpp

但在运行可执行文件时出现此错误

terminate called after throwing an instance of 'boost::wrapexcept<boost::wave::preprocess_exception>'
  what():  boost::wave::preprocess_exception
Aborted (core dumped)

正如注释中所建议的,在将std::cout << (*first).get_value();更改为std::cout << (*first).get_value() << ' ';时,显然会在EOF抛出异常。纯属猜测,我尝试在input末尾添加 ,程序运行正常,没有异常。

但是,当我将代码还原为链接的示例,以便从文件中读取input而不是硬编码时,

    //std::string input("int main() { std::cout << "Hello world"; }");
    std::ifstream instream("somefile.cpp");
    std::string input(
        std::istreambuf_iterator<char>(instream.rdbuf()),
        std::istreambuf_iterator<char>());

然后我收到相同的错误。


一个问题似乎与字符串末尾缺少的 有关,因此请更改此

    std::string input("int main() { std::cout << "Hello world"; }");

至此

    std::string input("int main() { std::cout << "Hello world"; }
");

似乎解决了这个问题。只是问题比这更广泛。实际上,如果我在字符串中添加一个标题:

    std::string input("#include<iostream>
int main() { std::cout << "Hello world"; }
");

然后我得到同样的失败。最令人惊讶的是,如果我注释该标题:

    std::string input("//#include<iostream>
int main() { std::cout << "Hello world"; }
");

然后程序运行,但输出为:

#line 2 "/home/enrico/preproc.cpp"
int main() { std::cout << "Hello world"; }

我完全不知道#include<iostream>是怎么变成#line 2 "/home/enrico/preproc.cpp"的。

推荐答案

技术上不带换行符的C源文件is invalid。您必须确保末尾有换行符:

std::string input("int main() { std::cout << "Hello world"; }
");

这篇关于如何运行Wave快速入门示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

error when trying to run an overloaded function with float parameters.(尝试运行带有浮点参数的重载函数时出错。)
C++ lt;lt; operator overloading without friend function(没有友元函数的C++lt;lt;运算符重载)
C++ - How does the compiler decide between overloaded functions with reference types as parameter?(C++-编译器如何决定使用引用类型作为参数的重载函数?)
How can I invoke an overloaded () operator on the keyword this?(如何对关键字this调用重载()运算符?)
How do I denote a pure virtual function in a UML class diagram?(如何在UML类图中表示纯虚函数?)
MPI parallel IO in ASCII format (How do I do it?)(ASCII格式的MPI并行IO(我该怎么做?))