我正在使用 C Builder XE6 并撰寫了以下 Delphi 單元:
unit JSONUtils;
interface
uses
System.JSON, System.Math;
function GetJSONDouble (Value: TJSONValue; Path: string; Default: Double = Infinity): Double;
implementation
function GetJSONDouble (Value: TJSONValue; Path: string; Default: Double): Double;
begin
Result := Value.GetValue<Double>(Path, Default);
end;
end.
編譯后,會生成以下 .hpp 檔案:
// CodeGear C Builder
// Copyright (c) 1995, 2014 by Embarcadero Technologies, Inc.
// All rights reserved
// (DO NOT EDIT: machine generated header) 'JSONUtils.pas' rev: 27.00 (Windows)
#ifndef JsonutilsHPP
#define JsonutilsHPP
#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
#include <System.JSON.hpp> // Pascal unit
#include <System.Math.hpp> // Pascal unit
//-- user supplied -----------------------------------------------------------
namespace Jsonutils
{
//-- type declarations -------------------------------------------------------
//-- var, const, procedure ---------------------------------------------------
extern DELPHI_PACKAGE double __fastcall GetJSONDouble(System::Json::TJSONValue* Value, System::UnicodeString Path, double Default = INF);
} /* namespace Jsonutils */
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_JSONUTILS)
using namespace Jsonutils;
#endif
#pragma pack(pop)
#pragma option pop
#pragma delphiheader end.
//-- end unit ----------------------------------------------------------------
#endif // JsonutilsHPP
請注意,檔案中的默認值Infinity被.pas轉換為 INF檔案中.hpp。
當我將.hpp檔案包含在 C 單元中時,我收到以下編譯器錯誤:
[bcc32 Error] JSONUtils.hpp(26): E2451 Undefined symbol 'INF'
可以理解,因為INF在 中沒有定義System.Math.hpp,但是Infinity是。
如何讓編譯器輸出Infinity(或HUGE_VAL)到.hpp檔案而不是 INF?
uj5u.com熱心網友回復:
如何讓編譯器輸出
Infinity(或HUGE_VAL)到.hpp檔案而不是INF?
AFAIK,你沒有。這就是 Delphi 編譯器選擇Infinity為 C 翻譯的方式。隨時向 Embarcadero 提交一份關于它的錯誤報告。
同時,作為一種解決方法,您可以嘗試GetJSONDouble使用{$NODEFINE}或{$EXTERNALSYM}避免 Delphi 編譯器在 中輸出默認宣告.hpp,然后用于{$HPPEMIT}宣告GetJSONDouble()自己,例如:
unit JSONUtils;
interface
uses
System.JSON, System.Math;
{$EXTERNALSYM GetJSONDouble}
function GetJSONDouble (Value: TJSONValue; Path: string; Default: Double = Infinity): Double;
{$HPPEMIT OPENNAMESPACE}
{$HPPEMIT 'extern DELPHI_PACKAGE double __fastcall GetJSONDouble(System::Json::TJSONValue* Value, System::UnicodeString Path, double Default = System::Math::Infinity);'}
{$HPPEMIT CLOSENAMESPACE}
implementation
function GetJSONDouble (Value: TJSONValue; Path: string; Default: Double): Double;
begin
Result := Value.GetValue<Double>(Path, Default);
end;
end.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/450615.html
