c++ - fcntl 问题编译 C++

标签 c++ gcc fcntl

{net04:~/xxxx/wip} gcc -o  write_test write_test.c
In file included from write_test.c:4:
global.h:10: warning: `b' initialized and declared `extern'

此代码使用 fcntl.h 和定义的文件处理函数 - 如 open()、write()、close() 等。 代码按预期编译和工作。

{net04:~/xxxx/wip} gcc -o  write_test write_test.cpp
In file included from write_test.cpp:4:
global.h:10: warning: `b' initialized and declared `extern'
write_test.cpp: In function `int main()':
write_test.cpp:56: error: `exit' undeclared (first use this function)
write_test.cpp:56: error: (Each undeclared identifier is reported only once for each function it appears in.)
write_test.cpp:58: error: `write' undeclared (first use this function)
write_test.cpp:62: error: `close' undeclared (first use this function)

当我将它用作 CPP 源代码时,为什么 GCC 会提示?奇怪的是,为什么它不提示 open()?这里到底发生了什么?

最佳答案

  1. C++ 对 header 的要求更为严格 - 您需要:#include <unistd.h>以正确获得指示的功能。

  2. global.h不应该定义 b - 标题不应该初始化变量。

  3. 编译时你应该使用-Wall -Werror这将迫使您修复代码中所有不可靠的部分。

  4. 获取exit()干净利落你需要#include <cstdlib> (C++) 或 #include <stdlib.h> (三)

  5. 使用 g++链接 C++ 代码,以便包含 C++ 库。使用 g++ 进行整个 C++ 编译可能是最简单的.

关于c++ - fcntl 问题编译 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1740640/

相关文章:

c++ - C++ 中 ü 的 ASCii 值

C++循环,输入的最大/最小数字

c++ - 如何通过opencv计算提取轮廓的曲率?

c++ - 编译时的类型特化

C:如何在不使用标准 I/O 库的情况下将数据附加到文件

ruby - 如何使用 File#flock 对独占锁发出非阻塞请求?

c - read() 在有效的文件描述符上返回错误的文件描述符

c++ - 在 C++ 中使用数组调用方法

linux - 获取加载的 dylib 的句柄

c - 如何使用包含线程的个人库编译 C 程序