問題描述
我在 VS2010 中有靜態庫 (.lib) 并將其鏈接到我的測試項目.
I have static library (.lib) in VS2010 and am linking it to my test project.
該庫有一個我使用以下宏創建的工廠:
The lib has a factory that I create using the below MACRO:
#define REGISTER_FACTORY(mType, my_class)
class Factory##my_class : public CAbstractFactory
{
public:
Factory##my_class() : CAbstractFactory(mType){}
CBaseClass *Create()
{ return new my_class(); }
};
static Factory##my_class StaticFactory##my_class;
應該發生的是,在 CAbstractFactory 中,新工廠通過 mtype
注冊.但是當我檢查工廠時,工廠不存在.
What is supposed to happen is that in the CAbstractFactory the new factory gets registered by mtype
. But when I check the factory the factory does not exist.
當我使用 DLL 而不是 .lib 時它工作正常.我的猜測是鏈接器不包含靜態變量,因為它沒有被引用,或者靜態變量甚至沒有包含在庫中.
It works fine when I use a DLL instead of a .lib. My guess is that the linker does not include the static variable as it is not referenced or the static variable was not even included in the library.
如何強制鏈接器將靜態庫中的所有對象包含在我的 .exe 中.
How can I force the linker to include all objects from the static library in my .exe.
我像這樣使用宏:
// Register factory that can create CMyObject with ID=100
REGISTER_FACTORY(100, CMyObject);
class CMyObject
{
};
CAbstractFactory 看起來像這樣:
The CAbstractFactory looks like this:
class CAbstractFactory {
CAbstractFactory(int id) {
CFactory::instance().add(id, this);
}
}
然后是代碼中的其他地方,我使用的主要 .exe:
Then some where else in the code, my main .exe I use:
CBaseClass *pObject = CFactory::instance().Create(100);
這會給我一個新的CMyObject
.這個想法是我有許多不同種類的對象,我有一個包含指定我需要的對象種類的 id 的數據庫.100
只是一個例子.
This will then give my a new CMyObject
. The idea is that I have many different kind object and I have a database containing the id specifying the kind of objects I need.
100
is just an example.
確實,我沒有直接從 .lib 中引用任何內容,但我希望能夠使用我的工廠創建對象
So indeed, I do not reference anything from the .lib directly but I want to be able to create the objects using my factory
CFactory 類是一個簡單的類,它保存所有 CAbstractFactory 類的寄存器(在映射中)并將 create 方法委托給正確的工廠.
The CFactory class is a simple class that keeps a register (in a map) of all the CAbstractFactory classes and delegates the create method to the correct factory.
CFactory &CFactory::Instance()
{
static CFactory instance;
return instance;
}
主要問題在于我沒有從 .lib 中引用任何內容,因為它都是通過 CFactory 完成的.如果我將其設置為 DLL 并確保我添加了對這個 DLL 的一些引用以確保它被加載,它就可以工作.但是對于 .lib,我什至添加了一個虛擬函數,以確保我至少有一個不包含其余代碼的引用.
The main problem lies in the fact that I do not reference anything from the .lib as it is all done through the CFactory. It works if I make it a DLL and make sure I add some reference to this DLL to make sure it is loaded. But for a .lib, I even added a dummy function to make sure I have at least one reference that doesn't include the rest of the code.
推薦答案
我遇到了類似的問題,通過將 lib 項目設置為主應用項目的依賴項,然后設置鏈接庫依賴項"和使用庫"來解決主要項目的依賴項輸入'為是.
I had a similar problem and solved it by setting the lib project as a dependency of the main app project and then setting 'Link Library Dependencies' and 'Use Library Dependency Inputs' to Yes for the main project.
更新:
最近我發現 Visual Studio 2015 現在支持 /WHOLEARCHIVE
鏈接器標志.我無法通過鏈接器選項找到它,但您可以將其添加為附加命令行選項.它的工作原理類似于 GCC 標志 -whole-archive
并將其添加到目標鏈接器標志(而不是靜態庫標志).
Recently I discovered that Visual Studio 2015 now supports a /WHOLEARCHIVE
linker flag. I can't find it through the linker options, but you can add it as an additional command line option. It works similar to the GCC flag -whole-archive
and you add it to your target linker flags (not to the static lib flags).
例如,指定 /WHOLEARCHIVE:lib_name
作為附加鏈接器命令行選項,它將包含該庫中的所有符號.您也可以執行多個庫.
For example, specify /WHOLEARCHIVE:lib_name
as an additional linker command line option and it will include all symbols from that lib. You can do this more than one lib as well.
如果您使用此 /WHOLEARCHIVE:lib_name
,則不再需要將鏈接庫依賴項"和使用庫依賴項輸入"設置為是".這非常適合通過 CMAKE 生成的解決方案.在此處查看相關答案:https://stackoverflow.com/a/42083877/1151329
If you use this /WHOLEARCHIVE:lib_name
you no longer need the 'Link Library Dependencies' and 'Use Library Dependency Inputs' set to Yes. This is perfect for solutions generated through CMAKE. See a related answer here: https://stackoverflow.com/a/42083877/1151329
這篇關于.lib 中的 C++ 靜態變量未初始化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!