2

我创建了一个 *.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 的 Requires 部分。但是,在构建 BPL_B 时出现以下链接错误。

[ILINK32 错误] 错误:在模块 xxx.OBJ 中导出 SomeClass::CreateUI() 引用单元 BPL_A]Form_Sub 中的 __fastcall TForm_Sub::TForm_Sub()。

我无法弄清楚缺少什么。

4

1 回答 1

3

尝试将#pragma package(smart_init)指令添加到源 (xxx.cpp) 文件中。

According to the 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.

于 2016-04-20T13:47:03.063 回答