使用boost(C++库)读取执行命令的结果
reading the result of executed command with boost (C++ Libraries)
我需要帮助。试图执行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错误),所以你需要验证你:
- 已构建boost库
- 已将boost iostream lib包含在您的makefile中
这应该会有所帮助,或者至少可以修复一些错误。
Boost.Iotstreams不是一个只有头的库。您需要通过在链接命令中添加以下内容来链接到它:-lboost-iostreams
。
相关文章:
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如何在 Minecraft 服务器上执行命令
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- 如何查看完整的CMD执行命令,包括参数和标志?
- CPP 中 z3 的编译和执行命令?
- 线程保持运行并执行命令
- 在 MinGW 和 MinGW-64 上执行命令后变量为空?
- popen() 将执行命令的输出写入 cout
- 如何在 Heroku 中的 Python 服务器上执行 "g++" 命令?
- 以普通用户身份执行命令
- 如何在生成之前在 cmake 中执行命令?
- 如何使用 c++ 在 cmd 中使用管理员权限执行命令
- 如何在Unix Shell中的后台执行命令
- 如何从连接的控制台执行命令
- 尝试在路由器上执行命令。C++ & libssh
- 在服务器上执行命令,然后将结果发送到Windows中的客户端
- 通过C编程打开终端并执行命令
- 在远程服务器上执行命令行命令
- 在Linux上使用c++执行命令时被阻止
- 使用popen执行命令