我需要这方面的帮助。尝试从用 C++ 开发的应用程序执行命令,我想直接读取结果。
- 操作系统:Debian 7 Wheezy
- boost_ver:1.49(来自 debian 存储库)
- 编译器:g++
我已经使我的程序尽可能短以识别问题。
基本上我试图做这样的事情。
但我的问题出在其他地方,我想我不知道如何正确使用 boost 库(因为这是我第一次使用其他 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,但不能生成可执行文件。我的包含是否正确?我认为可能有问题。
我不会发布结果make
,因为它很长,基本上它显示了一些链接问题。但如果你愿意,我可以编辑并发布它。两个代码都应该是可执行的。
谢谢!
编辑:
所以结果make
是:
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 的错误消息,但看起来非常相似。