【问题标题】:C++ builder package export link errorC++ builder包导出链接错误
【发布时间】:2016-04-20 09:02:25
【问题描述】:

我创建了一个 *.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()。

我不知道缺少什么。

【问题讨论】:

    标签: linker-errors c++builder bpl


    【解决方案1】:

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

    根据C++builder help

    在模块'module'中导出'symbol'在单元'unit'中引用'symbol'

    您正试图从不是单元的模块中导出符号(不包含#pragma package(smart_init) 指令),并且它引用了单元中的符号。这是不允许的,因为如果您有这样的符号,那么有人可以链接到它的导入;并且当调用导入时,它会调用单元代码。如果导出的非单元函数的客户端没有引用单元中的任何内容,它将永远不会被初始化。

    【讨论】:

    • 非常感谢,@manlio。我在 SomeClass .cpp 文件前面添加#pragma package(smart_init) directive,它可以工作。
    猜你喜欢
    • 2015-07-05
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多