0
我有文件的這種結構在我的DLL項目:重複的定義
// common_header.h //////////////////
extern int CommonVar = 0;
extern bool CommonVar2 = false;
EXPORT_API void ThisFunction();
EXPORT_API bool ThisOtherFunction();
// library_part1.cpp ////////////////
#include "common_header.h"
EXPORT_API void ThisFunction() {
if (CommonVar2) CommonVar++;
}
// library_part2.cpp ////////////////
#include "common_header.h"
EXPORT_API bool ThisOtherFunction() {
if (CommonVar>2) return true;
return false;
}
正如我所說的,我使用Microsoft Visual Studio構建這個成一個DLL,當然,我得到的鏈接錯誤與這些變量在對象文件中發現兩次有關。這是因爲它爲每個.cpp創建一個.obj,以及每個.cpp如何包含相同的頭文件也會導出變量。你懂了。現在,我想知道是否有解決方案,這樣我可以保留這兩個變量共同爲.CPP,也extern(我的意思是,能夠閱讀它們,至少,從應用程序使用該DLL)?當然,它們只能在.obj文件中聲明一次。也許有一些預編譯命令(如#pragma once
的標題)