{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()?这里到底发生了什么?
最佳答案
C++ 对 header 的要求更为严格 - 您需要:
#include <unistd.h>
以正确获得指示的功能。global.h
不应该定义 b - 标题不应该初始化变量。编译时你应该使用
-Wall -Werror
这将迫使您修复代码中所有不可靠的部分。获取
exit()
干净利落你需要#include <cstdlib>
(C++) 或#include <stdlib.h>
(三)使用
g++
链接 C++ 代码,以便包含 C++ 库。使用g++
进行整个 C++ 编译可能是最简单的.
关于c++ - fcntl 问题编译 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1740640/