質問
{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 ++はヘッダーについてより厳格です - あなたは必要です:
#include <unistd.h>
指定された関数を適切に取得します。global.h
B-ヘッダーが変数を初期化してはならないことを定義してはなりません。コンパイルするときは、使用する必要があります
-Wall -Werror
そして、それはあなたがあなたのコードのすべての危険なビットを修正することを強制します。取得するため
exit()
きれいに必要です#include <cstdlib>
(C ++)または#include <stdlib.h>
(c)使用する
g++
C ++コードをリンクして、C ++ライブラリが含まれるようにします。おそらくC ++コンパイル全体を実行するのが最も簡単ですg++
.
所属していません StackOverflow