爲什麼不能訪問file.cpp文件中定義的變量值。假設它與main鏈接。棘手的部分是變量值是間接包含的。主函數包含header1.h,其中包含file.h,其中的變量值爲extern。 可以EXTERN在報頭的鏈被傳播這樣頭文件層次結構中的extern變量
1)file.h
extern int value;
2)file.cpp
#include "file.h"
int value = 25;
3)那麼header1.h
#include "file.h"
const int const_value = 100;
4)main.cpp
#include "header1.h"
int main(char *argv[], int args) {
int result = value*10;
return result;
}
準確地說你會得到什麼錯誤?你確定file.cpp和main.cpp中的對象是否被賦予鏈接器? – aschepler