{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'
Run Code Online (Sandbox Code Playgroud)
此代码使用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)
Run Code Online (Sandbox Code Playgroud)
当我将它用作CPP源代码时,为什么GCC会抱怨?奇怪的是,为什么它不抱怨open()?这里甚至发生了什么?
C++对标题更严格 - 您需要:#include <unistd.h>正确获取指示的函数.
global.h 不应该定义b - 标题不应该初始化变量.
编译时你应该使用-Wall -Werror,这将迫使你修复代码的所有狡猾的位.
要exit()干净利落,你需要#include <cstdlib>(C++)或#include <stdlib.h>(C)
使用g++使C++库获得包括链接C++代码.可能最简单的做整个C++编译g++.