How can I run Wave quick start example?(如何运行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快速入门示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何运行Wave快速入门示例?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
