2016-04-20 37 views
1

我創建了一個* .bpl項目BPL_A它包含一個TForm子類,說TForm_SubC++ builder包導出鏈接錯誤

頭文件Form_Sub.h就像是以下幾點:

class PACKAGE TForm_Sub : public TForm 
{ 
    ... 
}; 

extern PACKAGE TForm_Sub* Form_Sub; 

源文件Form_Sub.cpp就像是以下幾點:

TForm_Sub* Form_Sub; 

__fastcall TForm_Sub::TForm_Sub(TComponent* Owner) 
{ 
    ... 
} 

我創建另一個* .bpl項目BPL_B動態創建TForm_Sub實例。

class PACKAGE SomeClass 
{ 
    public: 
     TForm* CreateUI(const AnsiString& name); 
}; 

#include "Form_Sub.h" 

TForm* SomeClass::CreateUI(const AnsiString& name) 
{ 
    if(name == xxx) 
    { 
     if(Form_Sub != NULL) 
     { 
     Form_Sub = new TForm_Sub(owner); 
     } 
     return Form_Sub; 
    } 
} 

我將BPL_A.bpi添加到BPL_B的需要部分。但是,構建BPL_B時出現以下鏈接錯誤。

[ILINK32 Error] Error: Export SomeClass::CreateUI() in module xxx.OBJ references __fastcall TForm_Sub::TForm_Sub() in unit BPL_A]Form_Sub.

我找不出什麼是缺失。

回答

2

嘗試將#pragma package(smart_init)指令添加到源文件(xxx.cpp)。

按照C++builder help

Export 'symbol' in module 'module' references 'symbol' in unit 'unit'

You are attempting to export a symbol from a module that is not a unit (does not contain the #pragma package(smart_init) directive) and it references a symbol in a unit. This is not allowed because if you have such a symbol, then someone can link to its import; and when the import is called, it calls into the unit code. If the client of the exported non-unit function did not reference anything from the unit, it will never be initialized.

+0

非常感謝,@manlio。我在SomeClass .cpp文件前添加'#pragma package(smart_init)指令',它可以工作。 – odomchen