Linux 上的 C++ 无法识别 exit() 和 printf() 等命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3956636/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:42:21  来源:igfitidea点击:

C++ on Linux not recognizing commands like exit() and printf()

c++linux

提问by Max

I get these errors after issuing a g++ command on a .cpp file: error: ‘exit' was not declared in this scope error: ‘printf' was not declared in this scope

在 .cpp 文件上发出 g++ 命令后,我收到这些错误:错误:'exit' 未在此范围内声明错误:'printf' 未在此范围内声明

The problem is that when I compiled this program on another linux machine, everything went fine. I tried searching around, but all I found was that I need to include files like 'stdlib.h'.

问题是当我在另一台 linux 机器上编译这个程序时,一切都很好。我试着四处搜索,但我发现我需要包含像“stdlib.h”这样的文件。

Could it be I'm missing some library on my OS? If so, what might it be?

难道我的操作系统上缺少一些库?如果是这样,那可能是什么?

采纳答案by Ignacio Vazquez-Abrams

Recent versions of GCC have gotten stricter in what responsibilities the programmer needs to fulfill. Include the cstdlib, cstdio, etc. header and access these functions from the stdnamespace.

最新版本的 GCC 在程序员需要履行的职责方面变得更加严格。包括cstdlibcstdio等头部和访问从这些函数std的命名空间。

回答by shuttle87

If you are in need of a quick (and dirty) fix try:

如果您需要快速(且肮脏)的修复,请尝试:

using namespace std;

Also make sure you are including the appropriate io headers.

还要确保包含适当的 io 标头。

回答by sanjeev

In terminal :- when you write man exit :- it will show

在终端中:- 当您编写 man exit 时:- 它将显示

   exit - cause normal process termination

SYNOPSIS #include stdlib.h

概要#include stdlib.h

   void exit(int status);

so you required to define header #include stdlib.h

所以你需要定义头 #include stdlib.h

回答by Mike S

Specifically for ‘exit' was not declared in this scopeall you need is:

特别适合‘exit' was not declared in this scope您需要的是:

#include <stdlib.h>