将std :: filebuf(FILE *)转换为使用libc ++ [英] Convert std::filebuf(FILE*) to use libc++

查看:809
本文介绍了将std :: filebuf(FILE *)转换为使用libc ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些现有代码正在尝试使用clang 3.3和libc ++从 llvm.org 进行编译。简单的步骤即可检索另一个命令的结果。看来std :: filebuf不再提供FILE *构造函数,并且我尝试替换代码的所有想法都无法打开命令,即fb.is_open()始终返回false。 p>

根据我的发现,我将不得不使用 fb.open(cppcommand.c_str(),std :: ios :: in)之类的东西; 而不是popen。



代码的基本部分是:-

  std :: string cppcommand = / usr / bin / cpp -xc -nostdinc test.c; 

文件* cpppipe = popen(cppcommand.c_str(), r);
std :: filebuf fb(cpppipe);

if(!cpppipe ||!fb.is_open()){
std :: cerr<< 无法运行’<< cppcommand.c_str()<< ’\n;
返回false;
} else {
std :: istream in(& fb);

std :: ostringstream ss;
ss<< in.rdbuf();
结果= ss.str();
}

如何使用libc ++来运行它?



代码来自 OpenShadingLanguage ,然后在从基本安装中删除gcc和libstdc ++之后,尝试在FreeBSD 10.0 Beta1(包含clang 3.3和libc ++)下进行编译。



cppcommand

解决方案

实际上 std :: filebuf 从未提出使用 FILE * 的构造函数。您已经成为gcc扩展的受害者。



C ++ I / O系统具有很好的可扩展性,尽管它相当古老。创建一个完全可以移植的C ++中的自定义 streambuf 并不困难,它可以由 FILE * 构造。通常,我只是将代码放到这里。但是,答案很长。通常,我不会无耻地插入产品而不是提供答案。



在这种情况下,我是一个例外。



Josuttis' C ++标准库 显示了如何针对POSIX文件描述符执行此操作第15.13.3。节采用此代码使用 FILE * 代替POSIX文件描述符将是微不足道的。



这是您唯一可以从Nicolai的书中获得的东西,我可能不会推荐。但是,事实并非如此。我推荐这本书。


I have some existing code that I am trying to compile using clang 3.3 and libc++ from llvm.org. A simple step to retrieve the result of another command. It appears that std::filebuf doesn't offer a FILE* constructor any more and all the ideas that I have tried to replace the code have all failed to open the command, that is fb.is_open() always returns false.

From what I can find I would have to use something like fb.open(cppcommand.c_str(), std::ios::in); instead of popen.

The essential parts of the code are :-

std::string cppcommand = "/usr/bin/cpp -xc -nostdinc test.c";

FILE *cpppipe = popen (cppcommand.c_str(), "r");
std::filebuf fb (cpppipe);

if (! cpppipe || ! fb.is_open()) {
    std::cerr << "Could not run '" << cppcommand.c_str() << "'\n";
    return false;
} else {
    std::istream in (&fb);

    std::ostringstream ss;
    ss << in.rdbuf();
    result = ss.str();
}

How can I get this running with libc++?

The code is from OpenShadingLanguage and I am trying to get it to compile under FreeBSD 10.0 Beta1 which contains clang 3.3 and libc++ after removing gcc and libstdc++ from the base install.

The cppcommand string being used runs without error if manually pasted into the terminal.

解决方案

Actually std::filebuf has never offered a constructor taking a FILE*. You've fallen victim to a gcc extension.

The C++ I/O system is very extensible, though in a fairly antique fashion. It is not that difficult to create a custom streambuf which could be constructed from a FILE*, in perfectly portable C++. Normally I'd just plop the code down here. However it is a little long for an answer. And normally I don't shamelessly plug a product instead of offering an answer.

In this case I'm making an exception.

Josuttis' "The C++ Standard Library" shows how to do this for a POSIX file descriptor in section 15.13.3. It would be trivial to adopt this code to use a FILE* instead of a POSIX file descriptor.

If this was the only thing you could get out of Nicolai's book, I probably wouldn't recommend it. However that is far from the case. I recommend this book.

这篇关于将std :: filebuf(FILE *)转换为使用libc ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆