使用boost(C++库)读取执行命令的结果

reading the result of executed command with boost (C++ Libraries)

本文关键字:执行 命令 结果 读取 boost C++ 使用      更新时间:2023-10-16

我需要帮助。试图执行C++中开发的应用程序中的命令,我想直接读取结果。

  • 操作系统:Debian 7 Wheezy
  • boost_ver:1.49(来自debian存储库)
  • 编译器:g++

我已经尽可能缩短了我的程序来识别问题。

基本上我想做这样的事。

但我的问题在于其他地方,我想我不知道如何正确使用boost库(因为这是我第一次使用other-then-std)。

长话短说,这里是我的代码:

fd.cpp:

//STD libraries
#include <iostream>
//BOOST libraries
#include <stream.hpp>
#include <file_descriptor.hpp>
typedef boost::iostreams::stream  boost::iostreams::file_descriptor_sink>
        boost_stream;
int main() {
    FILE *file;
    file = popen("./dumm", "r");
    if (!file) {
        return 7;
    }
    boost_stream bs;
    pclose(file);
}

和我的Makefile:

CPPC=g++
FLAGS=-Wall -fpermissive
INC=-I/usr/include/boost/iostreams/ -I/usr/include/boost/iostreams/device/
SOURCES=fd.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXE=x
all: $(EXE)
$(EXE): clean fd.o
    $(CPPC) $(OBJECTS) -o $@
fd.o:
    $(CPPC) $(FLAGS) $(INC) -c $(SOURCES) -o $@
clean:
    rm -f $(EXE)

我能够编译fd.o,但不能生成可执行文件。我的includes正确吗?我认为可能有问题。

我不会发布make的结果,因为它很长,基本上它显示了链接的一些问题。但如果你愿意,我可以编辑&张贴它。这两个代码都应该是可执行的。

谢谢!

编辑:

所以CCD_ 2的结果是:

fd.o: In function `int boost::iostreams::detail::read_device_impl<boost::iostreams::input>::read<boost::iostreams::file_descriptor>(boost::iostreams::file_descriptor&, boost::iostreams::char_type_of<boost::iostreams::file_descriptor>::type*, int)':
fd.cpp:(.text._ZN5boost9iostreams6detail16read_device_implINS0_5inputEE4readINS0_15file_descriptorEEEiRT_PNS0_12char_type_ofIS7_E4typeEi[_ZN5boost9iostreams6detail16read_device_implINS0_5inputEE4readINS0_15file_descriptorEEEiRT_PNS0_12char_type_ofIS7_E4typeEi]+0x1b): undefined reference to `boost::iostreams::file_descriptor::read(char*, int)'
fd.o: In function `int boost::iostreams::detail::write_device_impl<boost::iostreams::output>::write<boost::iostreams::file_descriptor>(boost::iostreams::file_descriptor&, boost::iostreams::char_type_of<boost::iostreams::file_descriptor>::type const*, int)':
fd.cpp:(.text._ZN5boost9iostreams6detail17write_device_implINS0_6outputEE5writeINS0_15file_descriptorEEEiRT_PKNS0_12char_type_ofIS7_E4typeEi[_ZN5boost9iostreams6detail17write_device_implINS0_6outputEE5writeINS0_15file_descriptorEEEiRT_PKNS0_12char_type_ofIS7_E4typeEi]+0x1b): undefined reference to `boost::iostreams::file_descriptor::write(char const*, int)'
fd.o: In function `void boost::iostreams::detail::close_impl<boost::iostreams::closable_tag>::close<boost::iostreams::file_descriptor>(boost::iostreams::file_descriptor&, std::_Ios_Openmode)':
fd.cpp:(.text._ZN5boost9iostreams6detail10close_implINS0_12closable_tagEE5closeINS0_15file_descriptorEEEvRT_St13_Ios_Openmode[_ZN5boost9iostreams6detail10close_implINS0_12closable_tagEE5closeINS0_15file_descriptorEEEvRT_St13_Ios_Openmode]+0x17): undefined reference to `boost::iostreams::file_descriptor::close()'
fd.o: In function `std::fpos<__mbstate_t> boost::iostreams::detail::seek_device_impl<boost::iostreams::any_tag>::seek<boost::iostreams::file_descriptor>(boost::iostreams::file_descriptor&, long long, std::_Ios_Seekdir, std::_Ios_Openmode)':
fd.cpp:(.text._ZN5boost9iostreams6detail16seek_device_implINS0_7any_tagEE4seekINS0_15file_descriptorEEESt4fposI11__mbstate_tERT_xSt12_Ios_SeekdirSt13_Ios_Openmode[_ZN5boost9iostreams6detail16seek_device_implINS0_7any_tagEE4seekINS0_15file_descriptorEEESt4fposI11__mbstate_tERT_xSt12_Ios_SeekdirSt13_Ios_Openmode]+0x35): undefined reference to `boost::iostreams::file_descriptor::seek(long long, std::_Ios_Seekdir)'
collect2: error: ld returned 1 exit status
make: *** [x] Error 1

编辑2:

更新的Makefile:

    CPPC=g++
    FLAGS=-Wall -fpermissive
    INC=-I/usr/include/boost/iostreams/ -I/usr/include/boost/iostreams/device/ -L/usr/lib/
    SOURCES=fd.cpp
    OBJECTS=$(SOURCES:.cpp=.o)
    EXE=x
    all: $(EXE)
    $(EXE): clean fd.o
        $(CPPC) $(INC) $(OBJECTS) -o $@
    fd.o:
        $(CPPC) $(FLAGS) $(INC) -c $(SOURCES) -o $@
    clean:
        rm -f $(EXE)

它仍然不起作用。我并没有对新旧Makefile的错误信息进行区分,但看起来非常相似。

很难说没有看到实际错误,但原因之一可能是boost::iostream的某些部分需要链接到已编译的boost iostream库和regex。如果不是这样的话,请在这里只提供链接器错误,它会让我们更好地帮助你。

编辑我真的不熟悉G++如何表达缺失的lib,但我觉得这就是问题所在。

看起来boost自动链接功能在这里不起作用(否则你会得到带有lib文件名的显式丢失lib错误),所以你需要验证你:

  1. 已构建boost库
  2. 已将boost iostream lib包含在您的makefile中

这应该会有所帮助,或者至少可以修复一些错误。

Boost.Iotstreams不是一个只有头的库。您需要通过在链接命令中添加以下内容来链接到它:-lboost-iostreams