简体   繁体   English

Cygwin gcc编译在IDE中失败,抱怨未声明“退出”

[英]Cygwin gcc compiled fails in IDE complaining about 'exit' undeclared

When I compile a program using just 当我只使用编译程序时

gcc code.c

There are no messages, and an output file is generated successfully. 没有消息,并且成功生成了输出文件。 The outputted file works. 输出的文件有效。 However, when I try to the same cygwin installation's gcc compiler in an IDE (I've tried Netbeans and Dev-C++), I get the following errors 但是,当我在IDE中尝试相同的cygwin安装的gcc编译器(我尝试过Netbeans和Dev-C ++)时,出现以下错误

main.cpp:27: error: `exit' undeclared (first use this function)
main.cpp:27: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:77: error: `write' undeclared (first use this function)
main.cpp:78: error: `close' undeclared (first use this function)

I don't see what's different. 我看不出有什么不同。 Why does it not compile? 为什么不编译?

OK, the issue was that in the IDE, the file had a .cpp extension, whereas when I was compiling from a terminal, it had a .c extension. 好的,问题是在IDE中,文件的扩展名为.cpp,而当我从终端进行编译时,文件的扩展名为.c。 So, my new question is why does it not compile when it's treated as a c++ file. 因此,我的新问题是,当将其视为c ++文件时,为什么不编译。 Isn't C a subset of C++? C不是C ++的子集吗?

C++ is stricter then C. Where C allows you to call a function without a prototype, C++ does not allow this. C ++比C语言更严格。C允许您在没有原型的情况下调用函数,而C ++不允许这样做。

To solve the problem, you want to add: 要解决该问题,您要添加:

#include <stdlib.h>

Also, when compiling at the command line. 另外,在命令行编译时。 Make sure to use the -Wall flag so you'll get important warnings: 确保使用-Wall标志,这样您将收到重要警告:

gcc -Wall code.c

The IDE is using fussier options to the compiler. IDE对编译器使用了易用的选项。 You need to include some headers: 您需要包括一些标题:

#include <stdlib.h>  // exit()
#include <unistd.h>  // close(), write()

The default options allow almost anything that might be C to compile. 默认选项允许几乎所有可以使用C进行编译的东西。 By the looks of it, the IDE sets '-Wmissing-prototypes' as one of the compiler options. 从外观上看,IDE将“ -Wmissing-prototypes”设置为编译器选项之一。


If you compile code with a C++ compiler, you must ensure that all functions are declared before use. 如果使用C ++编译器编译代码,则必须确保在使用之前声明所有函数。 C is sloppier (or can be sloppier) about that - it is recommended practice to ensure all functions are declared before being defined or referenced, but it is not mandatory. C对此更精明(或可以更精简)-建议实践确保在定义或引用所有函数之前先声明所有函数,但这不是强制性的。 In C++ it is not optional. 在C ++中,它不是可选的。

There is a subset of C that is also a subset of C++; C的一个子集也是C ++的一个子集; there are bits of C that are not C++, and there are many bits of C++ that are not C. In particular, an arbitrary C program is not, in general, a C++ program. C的某些位不是C ++,C的很多位不是C。特别是,任意C程序通常不是C ++程序。 For example, a C program may not declare 'exit()' and yet it can both use it and still compile. 例如,C程序可能没有声明'exit()',但它既可以使用它,也可以编译。 A C++ program must declare 'exit()' before it can user it and compile. C ++程序必须先声明“ exit()”,然后才能使用它并进行编译。

您将必须使用g++来编译.cpp文件。

一种可能的原因可能是IDE无法访问包含文件,cygwin gcc编译器可能期望它在/usr/include (不确定),而dev-cpp可能无法访问它。

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

相关问题 gcc -Wstringop-overflow 在这里抱怨什么? - What is gcc -Wstringop-overflow complaining about here? 即使有要求,为什么 gcc 也不会抱怨数组边界? - Why isn't gcc complaining about array bounds even if requested? 使用Cygwin GCC编译的C ++程序是否有免费的内存调试器? - Is there a free memory debugger for C++ programs compiled with Cygwin GCC? 相同的代码由clang编译,但在gcc中失败 - The same code gets compiled by clang but fails in gcc Phyon在Cygwin上抱怨Qt编译时不支持-fvisibility = hidden - Phonon on Cygwin complains about Qt compiled without support for -fvisibility=hidden 与 GCC 的交叉编译在 CYGWIN 下失败时<iostream>已经包括了 - cross-compilation with GCC fails under CYGWIN when <iostream> is included OS X上的Eclipse CDT抱怨gcc内置函数:“函数&#39;__builtin_bzero&#39;无法解析” - Eclipse CDT on OS X complaining about gcc built-in function: “Function '__builtin_bzero' could not be resolved” 寻找cygwin.s的GCC 8.1.0 / MinGW64编译的OpenMP程序崩溃? - GCC 8.1.0/MinGW64-compiled OpenMP program crashes looking for cygwin.s? gcc未声明的标识符“ _asm” - gcc undeclared identifier “_asm” Eclipse无法识别CygWin gcc - Eclipse not recognizing CygWin gcc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM