如何让 Delphi 在 hpp 文件中发出函数和过程定义?

nac*_*bar 3 delphi c++builder

我正在尝试在 C++ Builder 项目中使用 Delphi 函数(因为 ImageEn 的 IEVision 组件没有可用于提取条形码的 C++ 接口,所以我需要在 Delphi 单元中提取条形码)

我创建了一个C++ Builder项目,并添加了一个Delphi Unit,其完整代码为:

unit Unit2;

interface

implementation
function MyOutput : String ;
begin
  Result := 'hello';
end;

end.
Run Code Online (Sandbox Code Playgroud)

我在 C++ 形式中使用该单元:

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String result = MyOutput();
}
Run Code Online (Sandbox Code Playgroud)

生成的 .hpp 是:

// CodeGear C++Builder
// Copyright (c) 1995, 2013 by Embarcadero Technologies, Inc.
// All rights reserved

// (DO NOT EDIT: machine generated header) 'Unit2.pas' rev: 26.00 (Windows)

#ifndef Unit2HPP
#define Unit2HPP

#pragma delphiheader begin
#pragma option push
#pragma option -w-      // All warnings off
#pragma option -Vx      // Zero-length empty class member 
#pragma pack(push,8)
#include <System.hpp>   // Pascal unit
#include <SysInit.hpp>  // Pascal unit

//-- user supplied -----------------------------------------------------------

namespace Unit2
{
//-- type declarations -------------------------------------------------------
//-- var, const, procedure ---------------------------------------------------
}   /* namespace Unit2 */
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_UNIT2)
using namespace Unit2;
#endif
#pragma pack(pop)
#pragma option pop

#pragma delphiheader end.
//-- end unit ----------------------------------------------------------------
#endif  // Unit2HPP
Run Code Online (Sandbox Code Playgroud)

请注意,MyOutput 函数的声明未包含在 .hpp 文件中。我如何让 Delphi 将该函数放入 .hpp

Dav*_*nan 5

生成的头文件只包含来自接口部分的符号。您仅在实现部分声明了该函数。该函数也不能从 Delphi 代码中调用。

您还必须在接口部分声明该函数,以便其他单元可以看到它。

unit Unit2;

interface

function MyOutput : String ;

implementation

function MyOutput : String ;
begin
  Result := 'hello';
end;

end.
Run Code Online (Sandbox Code Playgroud)