Re: FreePascal unit in Cocoa project - How?
Re: FreePascal unit in Cocoa project - How?
- Subject: Re: FreePascal unit in Cocoa project - How?
- From: Jonas Maebe <email@hidden>
- Date: Wed, 25 Feb 2009 11:31:01 +0100
On 25 Feb 2009, at 10:56, Alexander Bokovikov wrote:
I have a big stuff, written in Pascal, which I'd like to use in
Cocoa-based GUI application. Pascal code are classes, but they have
no relation to visual subsystem. Therefore, as I hope, that code
could be used as long as it is compatible with FPC 2.2.X.
That is perfectly possible.
My problem is that I can't find any XCode sample, where FPC unit
would be included into Cocoa project. What I've found, is everything
else -- a FPC XCode project, using C/C++/ObjC units, Cocoa headers,
translated to FPC, etc. All that is very interesting and
instructive, but it doesn't answer my main question.
So, is there anybody out there, who ever saw or used such trick, as
FPC unit inclusion into Cocoa application?
There is no specific FPC/Cocoa template, but I think this should work:
a) start from the "FPC-C-C++ Carbon Application" template shipped with
FPC
b) add a new target to the project, and pick Application->Cocoa as the
template type
c) add your Objective-C sources and nibs/xibs to the project and to
this Cocoa target (similarly, if you add additional Pascal sources add
them only to the target with the same name as your project, and
additional C/C++ sources only to the target with the name "External")
d) rename the "main" function in your Cocoa sources to "CocoaMain"
e) remove the "MainUnit.pas" unit from the project and replace the
contents of the Start.pas file with the following:
***
uses
math, ctypes;
procedure CocoaMain(_argc: cint; _argv: ppchar); cdecl; external;
begin
SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,
exOverflow, exUnderflow, exPrecision])
CocoaMain(argc,argv);
end.
***
This will, after initialising the Free Pascal run time library, first
disable all floating point exceptions (Mac OS X' GUI libraries are
littered with invalid floating point operations, so FPC's default
behaviour of enabling fpu exceptions wreaks havoc with that), and then
start your Objective-C program.
To call Pascal code from this Objective-C program, you
a) have to create a procedural interface to your Pascal code (make
sure you mark all such exported procedures using the "cdecl;" modifier
to make them use the C calling convention)
b) create C headers for this procedural interface (so the Objective-C
compiler can read the declarations)
c) use the functionality in this procedural interface from Objective-C
like regular external C code
When adding additional Pascal sources to the project, make sure they
are (only) added to the target with the same name as your project.
Jonas
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden