C++ 为什么 g++ 找不到 iostream.h?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13103108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't g++ find iostream.h?
提问by quakkels
I'm trying to understand how to compile C++ programs from the command line using g++ and (eventually) Clang on Ubuntu.
我试图了解如何在 Ubuntu 上使用 g++ 和(最终)Clang 从命令行编译 C++ 程序。
I found a webpage which explains MakeFiles and I am following their directions. http://mrbook.org/tutorials/make/
我找到了一个解释 MakeFiles 的网页,我正在按照他们的指示进行操作。http://mrbook.org/tutorials/make/
I downloaded the four example files into their own directory.
我将四个示例文件下载到它们自己的目录中。
- main.cpp
- hello.cpp
- factorial.cpp
- functions.h
- 主程序
- 你好.cpp
- 阶乘文件
- 函数.h
I then went ahead and ran their example of how to manually compile without a MakeFile.
然后我继续运行他们的示例,说明如何在没有 MakeFile 的情况下手动编译。
g++ main.cpp hello.cpp factorial.cpp -o hello
When I ran the command from above, I received the following error from g++:
当我从上面运行命令时,我从 g++ 收到以下错误:
main.cpp:1:22: fatal error: iostream.h: No such file or directory
compilation terminated.
hello.cpp:1:22: fatal error: iostream.h: No such file or directory
compilation terminated.
My only experience with writing c++ is using an IDE such as VS C++ Express or CodeBlocks. Isn't the compiler supposed to know what iostream.h is and where to find it?
我编写 C++ 的唯一经验是使用 IDE,例如 VS C++ Express 或 CodeBlocks。编译器不应该知道 iostream.h 是什么以及在哪里可以找到它吗?
How do I get rid of this error so the program willl compile?
我如何摆脱这个错误以便程序能够编译?
Thanks for any help.
谢谢你的帮助。
回答by Adam Rosenfield
Before the C++ language was standardized by the ISO, the header file was named <iostream.h>
, but when the C++98 standard was released, it was renamed to just <iostream>
(without the .h
). Change the code to use #include <iostream>
instead and it should compile.
在 C++ 语言被 ISO 标准化之前,头文件被命名为<iostream.h>
,但是当 C++98 标准发布时,它被重命名为 just <iostream>
(没有.h
)。更改要使用的代码#include <iostream>
,它应该可以编译。
You'll also need to add a using namespace std;
statement to each source file (or prefix each reference to an iostream function/object with a std::
specifier), since namespaces did not exist in the pre-standardized C++. C++98 put the standard library functions and objects inside the std
namespace.
您还需要向using namespace std;
每个源文件添加一条语句(或使用std::
说明符为每个对 iostream 函数/对象的引用添加前缀),因为在预标准化的 C++ 中不存在命名空间。C++98 将标准库函数和对象放在std
命名空间内。
回答by Cheers and hth. - Alf
<iostream.h>
has never been a standard C++ header, because it did not make it into the C++ standard.
<iostream.h>
从来都不是标准的 C++ 头文件,因为它没有进入 C++ 标准。
Instead we got <iostream>
, in 1998.
相反,我们<iostream>
在 1998 年得到了。
Steer well clear of teaching material using non-standard stuff such as <iostream.h>
or void main
.
避免使用非标准内容(例如<iostream.h>
或 )的教材void main
。
However, as a practical solution for your current pre-standard code, you may try to replace
但是,作为当前预标准代码的实用解决方案,您可以尝试替换
#include <iostream.h>
with
和
#include <iostream>
using namespace std;
It’s not guaranteed to work, but chances are that it will work.
它不能保证工作,但有可能它会工作。
回答by nodlehs
Another related issue that wasn't mentioned here, so I will include it for anyone's future reference, is from the command line the compiler needs the environment path variable updated to find the location of the c++ header files. In windows you can just update the path environment using the 'advanced system properties' GUI and add the location of the c++ include files. This will update the PATH environment variable in Windows cmd & Cygwin automatically upon restarting the shell.
此处未提及的另一个相关问题,因此我会将其包含在内以供将来参考,即从命令行编译器需要更新环境路径变量以查找 c++ 头文件的位置。在 Windows 中,您可以使用“高级系统属性”GUI 更新路径环境并添加 c++ 包含文件的位置。这将在重新启动 shell 时自动更新 Windows cmd 和 Cygwin 中的 PATH 环境变量。
To update your PATH from Linux or the Cygwin shell type... PATH=$PATH:/your_path_here Example:PATH=$PATH:/cygdrive/c/cygwin/lib/gcc/i686-pc-mingw32/4.7.3/include/c++ Also a good idea to add just the include directory as well: PATH=$PATH:/cygdrive/c/cygwin/lib/gcc/i686-pc-mingw32/4.7.3/include/ ...or check the proper directories for the location of your installation's include files, I recommend installing mingw for use with Cygwin, which is envoked with g++.
要从 Linux 或 Cygwin shell 类型更新 PATH... PATH=$PATH:/your_path_here 示例:PATH=$PATH:/cygdrive/c/cygwin/lib/gcc/i686-pc-mingw32/4.7.3/include /c++ 也是一个好主意,只添加包含目录:PATH=$PATH:/cygdrive/c/cygwin/lib/gcc/i686-pc-mingw32/4.7.3/include/ ...或检查正确的安装包含文件的位置的目录,我建议安装 mingw 以与 Cygwin 一起使用,它是用 g++ 调用的。
To install additional needed packages in Cygwin re-run the Cygwin install utility & check install from Internet to add packages from web repositories and add mingw-gcc-g++ & mingw-binutils. To compile: g++ hello.cpp -o hello
要在 Cygwin 中安装其他需要的软件包,请重新运行 Cygwin 安装实用程序并检查从 Internet 安装以从 Web 存储库添加软件包并添加 mingw-gcc-g++ 和 mingw-binutils。编译:g++ hello.cpp -o hello
If using the gcc utility instead compile with the command: gcc hello.cpp -o hello -lstdc++ ... to get your executable.
如果使用 gcc 实用程序而不是使用以下命令编译: gcc hello.cpp -o hello -lstdc++ ... 以获取您的可执行文件。
As long as you have either gcc or mingw installed and the path to the c++ include files is in your path environment variable, the commands will work.
只要您安装了 gcc 或 mingw 并且 C++ 包含文件的路径在您的路径环境变量中,这些命令就会起作用。