• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: FreePascal unit in Cocoa project - How?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
  • Follow-Ups:
    • Re: FreePascal unit in Cocoa project - How?
      • From: Jonas Maebe <email@hidden>
References: 
 >FreePascal unit in Cocoa project - How? (From: "Alexander Bokovikov" <email@hidden>)

  • Prev by Date: Re: What is the difference between linked frameworks and other frameworks in an Xcode project
  • Next by Date: Re: FreePascal unit in Cocoa project - How?
  • Previous by thread: FreePascal unit in Cocoa project - How?
  • Next by thread: Re: FreePascal unit in Cocoa project - How?
  • Index(es):
    • Date
    • Thread