linker-errorsc++builderbpl

C++ builder package export link error


I create a *.bpl project BPL_A which contain a TForm subclass, say TForm_Sub.

The header file Form_Sub.h is like following:

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

extern PACKAGE TForm_Sub* Form_Sub;

The source file Form_Sub.cpp is like following:

TForm_Sub* Form_Sub;

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

And I create another *.bpl project BPL_B to dynamically create TForm_Sub instance.

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;
   }
}

I add BPL_A.bpi to Requires section of BPL_B. However, I get following link error when building BPL_B.

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

I can not figure out what is missing.


Solution

  • Try adding the #pragma package(smart_init) directive to the source (xxx.cpp) file.

    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.