C++ 生成器程序包导出 link 错误

C++ builder package export link error

我创建了一个 *.bpl 项目 BPL_A,其中包含一个 TForm 子类,比如 TForm_Sub

头文件 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.

时出现以下 link 错误

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

我不知道缺少了什么。

尝试将 #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.