質問

{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 ++はヘッダーについてより厳格です - あなたは必要です: #include <unistd.h> 指定された関数を適切に取得します。

  2. global.h B-ヘッダーが変数を初期化してはならないことを定義してはなりません。

  3. コンパイルするときは、使用する必要があります -Wall -Werror そして、それはあなたがあなたのコードのすべての危険なビットを修正することを強制します。

  4. 取得するため exit() きれいに必要です #include <cstdlib> (C ++)または #include <stdlib.h> (c)

  5. 使用する g++ C ++コードをリンクして、C ++ライブラリが含まれるようにします。おそらくC ++コンパイル全体を実行するのが最も簡単です g++.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top