ファイル名:Constants.cpp
------------------------------
#include stdafx.h
const int count = 100;
------------------------------
ファイル名:Constants.h
------------------------------
#pragma once
extern const int count;
------------------------------
ファイル名:hoge.cpp
------------------------------
#include stdafx.h
#include Constants.h
int main(int argc, char* argv[])
{
int a = count;
return 0;
}
------------------------------
上記のファイルをビルドすると下記のエラーが発生します。
hoge.obj : error LNK2001: 外部シンボル "int const count (?count@@3HB) は未解決で
す
Debug/hoge.exe : fatal error LNK1120: 外部参照 1 が未解決です。
エラーの原因は何なのでしょう?
ご教授ください。
Win98SE VC++6,0SP5 コンソールアプリ
C++ の場合 const をつけるとデフォルトで内部リンケージになります。
Constants.cpp で、extern const int count = 100;
とすれば外部リンケージを持つようになります。
参考:
http://www.microsoft.com/japan/developer/library/default.asp?
URL=/japan/developer/library/vccore/_langref_constant_values.htm
リンクエラーは発生しなくなりました。
ありがとうございました。