掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
C++でのグローバル変数の宣言 (ID:51435)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
> 複数のソースファイルから使用できる変数の宣言についてですが、 > プロジェクト内に > main.cpp > sub.cpp > main.h > とあるとき、main.cppとsub.cppの両方で使用できる変数g_abcを、 > main.hに > int g_abc; > のように宣言し、main.cppとsub.cppでmain.hをインクルードしてビルドすると、 > "int g_abcはすでにsub.objで定義されています > 1 つ以上の複数回定義されているシンボルが見つかりました" > とエラーが出てしまいます。 C++標準(ISO/IEC 14882:1998)の3.2 One definition ruleの3より。 >Every program shall contain exactly one definition of every non-inline function or object that is used in that program; no diagnostic required. C標準(JIS X 3010:1993)の6.7 外部定義より >外部結合で宣言した識別子を式(sizeof演算子のオペランドである場合を除く。)の中で使用する場合,プログラム全体のどこかにその識別子に対する外部定義が必ず一つなければならない。使用しない場合,二つ以上あってはならない。 C標準(ISO/IEC 9899:1999)の6.9 External definitionの5より >If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one. JIS X 3010だと曖昧ですが,ISO/IEC 14882:1998や同9899:1999では外部定義は一つにする必要があります。 というわけで,どちらかで定義して,もう一方では宣言だけにするとよいです。 ヘッダファイルには,通常 extern int g_abc; のように外部宣言のみを書きます。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.