我正在为Delphi和C ++ Builder维护VirtualTreeView组件.使用Delphi,一切都很好,但是当我使用C ++ Builder编译软件包时,不会调用Delphi单元中初始化部分中的代码.有什么想法吗?
I'm maintaining the VirtualTreeView component for Delphi and C++Builder. With Delphi everything is okay but when I compile the packages with the C++Builder the code in the initialization part in the Delphi units is not called. Any ideas?
推荐答案在C ++ Builder项目中未调用Delphi单元的initialization/finalization部分时,通常意味着未在使用Delphi单元链接到最终可执行文件中,通常是因为C ++代码没有直接引用该单元中的任何代码,所以它得到了优化.与删除Delphi相比,C ++ Builder在删除未使用的代码方面更具侵略性.在Delphi中,只需在uses子句中添加一个单元就可以将该单元链接到.在C ++中不是这种情况.如果C ++代码未使用.hpp文件中的任何内容,则用C ++代码#include用Delphi单元的.hpp文件不足以保证已链接该单元.
When a Delphi unit's initialization/finalization sections are not being called in a C++Builder project, it usually means the Delphi unit is not being linked into the final executable, typically because the C++ code is not directly referencing any code in the unit, so it gets optimized out. C++Builder is a bit more aggressive about removing unused code than Delphi is. In Delp simply adding a unit to a uses clause forces that unit to be linked to. That is not the case in C++. #includeing a Delphi unit's .hpp file in C++ code is not enough to guarantee the unit is linked to, if the C++ code does not use anything from the .hpp file.
Indy在多个单元中都遇到了此问题,最显着的是IdAllAuthentications,IdAllFTPListParsers和IdAllHeaderCoders.这些单元都只包含初始化/完成代码,没有接口代码,因此它们生成的.hpp文件基本上是空的.为了强制链接,我必须在interface部分中添加{$HPPEMIT}语句,以在生成的.hpp文件中输出#pragma link语句.例如:
Indy ran into this problem in several of its units, most notably IdAllAuthentications, IdAllFTPListParsers, and IdAllHeaderCoders. These units all contain only initialization/finalization code, no interface code, so their generated .hpp files were basically empty. To force linkage, I had to add {$HPPEMIT} statements to the interface section to output #pragma link statements in the generated .hpp files. For example:
unit IdAllAuthentications; interface { Note that this unit is simply for listing ALL Authentications in Indy. The user could then add this unit to a uses clause in their program and have all Authentications linked into their program. ABSOLUTELY NO CODE is permitted in this unit. } {$I IdCompilerDefines.inc} // RLebeau 2/14/09: this forces C++Builder to link to this unit so // the units can register themselves correctly at program startup... {$IFDEF HAS_DIRECTIVE_HPPEMIT_LINKUNIT} {$HPPEMIT LINKUNIT} {$ELSE} {$HPPEMIT '#pragma link "IdAllAuthentications"'} {$ENDIF} implementation // uses units that self-register in their initialization sections ... end.{$HPPEMIT LINKUNIT} 在XE5 Update 2中引入,以帮助链接使用单位作用域名称的单位:
{$HPPEMIT LINKUNIT} was introduced in XE5 Update 2, to help with linking units that use unit-scope names:
新增功能:现在,您可以使用HPPEMIT Delphi编译器指令来链接和生成C ++名称空间声明.
New: You can now use HPPEMIT Delphi compiler directives for linking and generating C++ namespace declarations.
...
{$ HPPEMIT LINKUNIT} 替换了iOS设备目标平台的#pragma链接. 有关更多信息,请参见HPPEMIT.
{$HPPEMIT LINKUNIT} replaces #pragma link for the iOS device target platform. For more information, see HPPEMIT.
对于C ++应用程序,{$HPPEMIT LINKUNIT}替换了移动平台上的#pragma link.
For C++ applications, {$HPPEMIT LINKUNIT} replaces #pragma link on mobile platforms.
Delphi运行时具有必须链接的单元才能启用某些功能.在C ++中,以前使用以下指令实现了自动链接:
The Delphi run time has units that must be linked in order to enable some functionality. In C++, auto-linking was previously achieved using the following directive:
{$HPPEMIT '#pragma link "<unitname>"'}
现在您应该改为使用以下指令:
Now you should use the following directive instead:
{$HPPEMIT LINKUNIT}
LINKUNIT生成一个#pragma link语句,该语句使用正确的修饰/命名间隔的单元名称来引用调用单元.
LINKUNIT generates a #pragma link statement that references the calling unit using the correct decorated/namespaced unit name.
更多推荐
初始化部分不被调用
发布评论