简体   繁体   English

c++ 要包含哪些文件和库?

[英]c++ What files and libs to include?

Many times I find useful code examples on the Internet.很多时候我在 Internet 上找到有用的代码示例。 About half of the time they don't specify what files to include or even what libs to include on the command line with -l.大约有一半的时间,他们没有使用 -l 指定要包含哪些文件,甚至没有指定要在命令行中包含哪些库。 How do you usually find that out?你通常是怎么发现的?

edit note: The problem below has been solved.编辑说明:以下问题已解决。 The remainder of this post can be skipped.这篇文章的其余部分可以跳过。

Right now, I'm getting tons of errors while trying to compile:现在,我在尝试编译时遇到了很多错误:

53: string Gunzip::gunzip(string& compressed)
54: {
55:   namespace io = boost::iostreams;
56:
57:   io::filtering_istream gunzip;
58:   gunzip.push(io::gzip_decompressor());
59:   std::istringstream in_stream = std::istringstream(compressed);
60:   gunzip.push(in_stream);
61:
62:   stringstream strstream;
63:   io::copy(gunzip, strstream);
64:   return strstream.str();
65: }

After a day on the Internet I'm trying:在互联网上呆了一天后,我正在尝试:

option: 3 -L/usr/include/boost
and:
 8: #include <string>
 9: #include <iostream>
10: #include <sstream>

15: #include <boost/iostreams/copy.hpp>
16: #include <boost/iostreams/device/array.hpp>
17: #include <boost/iostreams/device/back_inserter.hpp>
18: #include <boost/iostreams/filter/gzip.hpp>
19: #include <boost/iostreams/filter/test.hpp>
20: #include <boost/iostreams/filtering_stream.hpp>

The error I have is:我的错误是:

                 from /usr/include/c++/4.5/string:45,
                 from Gunzip.cpp:8:
/usr/include/c++/4.5/bits/ios_base.h: In copy constructor     ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
In file included from /usr/include/c++/4.5/bits/localefwd.h:43:0,
/usr/include/c++/4.5/bits/ios_base.h:785:5: error: ‘std::ios_base::ios_base(const     std::ios_base&)’ is private
/usr/include/c++/4.5/iosfwd:77:11: error: within this context
/usr/include/c++/4.5/iosfwd: In copy constructor     ‘std::basic_istringstream<char>::basic_istringstream(const     std::basic_istringstream<char>&)’:
/usr/include/c++/4.5/iosfwd:97:11: note: synthesized method     ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here 
/usr/include/c++/4.5/streambuf: In copy constructor     ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’:
/usr/include/c++/4.5/streambuf:773:7: error: ‘std::basic_streambuf<_CharT,     _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>::__streambuf_type&)     [with _CharT = char, _Traits = std::char_traits<char>, std::basic_streambuf<_CharT,     _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private
/usr/include/c++/4.5/iosfwd:93:11: error: within this context
/usr/include/c++/4.5/iosfwd: In copy constructor     ‘std::basic_istringstream<char>::basic_istringstream(const     std::basic_istringstream<char>&)’:
/usr/include/c++/4.5/iosfwd:97:11: note: synthesized method     ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’ first     required here 
Gunzip.cpp: In member function ‘std::string Gunzip::gunzip(std::string&)’:
Gunzip.cpp:59:65: note: synthesized method     ‘std::basic_istringstream<char>::basic_istringstream(const std::basic_istringstream<char>&)’ first required here 
make[2]: Leaving directory `/home/albert/NetBeansProjects/Arb3'
make[1]: Leaving directory `/home/albert/NetBeansProjects/Arb3'
make[2]: *** [build/Debug/GNU-Linux-x86/Gunzip.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 9s)

I can remove the first 3 includes to obtain other errors I don't understand.我可以删除前 3 个包含以获取其他我不理解的错误。 I don't know which errors are better.我不知道哪个错误更好。

  1. Is this error related to includes?此错误与包含有关吗? How should I know?我应该怎么知道? I'm blaming the includes because so far all errors were due to includes.我责怪包含,因为到目前为止所有错误都是由于包含。 I don't know what basic_ios is.我不知道 basic_ios 是什么。
  2. How do you find out what to include and what libs to use?您如何找出要包含的内容和要使用的库?

The error you posted is actually not an include-related error, or a linker error it is the line:您发布的错误实际上不是与包含相关的错误,也不是 linker 错误,它是以下行:

std::istringstream in_stream = std::istringstream(compressed);

that causes a call to an inherited copy constructor that is marked as private, so you should replace it with:这会导致调用标记为私有的继承的复制构造函数,因此您应该将其替换为:

std::istringstream in_stream(compressed);

The compiler output is actually quite clear in this case.编译器 output 在这种情况下其实是很清楚的。

The short answer is 'it depends'.简短的回答是“视情况而定”。 For classes / functions which are part of the C++ standard library the man pages which come with your compiler will list which header(s) and libraries are required;对于属于 C++ 标准库的类/函数,编译器随附的手册页将列出所需的头文件和库; or alternately you can use online resources such as cplusplus.com , MSDN or GNU libstdc++ doxygen docs .或者,您也可以使用在线资源,例如cplusplus.comMSDNGNU libstdc++ doxygen docs

For things like Boost you have to look at their documentation;对于像 Boost 这样的东西,你必须查看他们的文档; however the obvious question is 'how do I know if a class is from boost' - to which the answer is pretty much 'Google it' - over time you'll get used to what is and isn't in Boost.然而,显而易见的问题是“我怎么知道 class 是否来自 boost”——答案几乎是“谷歌它”——随着时间的推移,你会习惯 Boost 中的内容。

In general if you could not find the required libraries or don't know how to build and use it, the best places to find such information are: the project's docs or wiki page, forum or IRC channel.一般来说,如果您找不到所需的库或不知道如何构建和使用它,找到此类信息的最佳位置是:项目的文档或 wiki 页面、论坛或 IRC 频道。 To find which project the classes/functions belong you can search using the class/function's name.要查找类/函数所属的项目,您可以使用类/函数的名称进行搜索。

For the above code snippet you need Boost .对于上面的代码片段,您需要Boost Mostly all you need to do is #include the header file.大多数情况下,您需要做的就是#include header 文件。 But certain libraries require you to build and link them.但是某些库需要您构建和链接它们。 See the ' Getting Started on Microsoft Windows ' or ' Getting Started on Unix variants (eg Linux, MacOS) ' for instructions.有关说明,请参阅“ Microsoft Windows 入门”或“ Unix 变体(例如 Linux、MacOS)入门”。

If you are working on a Windows machine, you can download the pre-compiled binaries from here: http://www.boostpro.com/download/如果您正在使用 Windows 机器,您可以从这里下载预编译的二进制文件: http://www.boostpro.com/download/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM