fcntl发布编译C ++的问题

时间:2009-11-16 07:33:25

标签: 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 个答案:

答案 0 :(得分:6)

  1. C ++对标题更严格 - 您需要:#include <unistd.h>才能正确获取所指示的功能。

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

  3. 编译时,您应使用-Wall -Werror,这将迫使您修复代码中所有狡猾的位。

  4. 要清楚地获得exit(),您需要#include <cstdlib>(C ++)或#include <stdlib.h>(C)

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