How to include Carbon.h?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

How to include Carbon.h?

BlueCat
Hello, Pascal folks,

I'm using FPC 2.0.4 on MacOSX 1.4.9 PPC and trying to write a Pascal library that is externally called from REALbasic (Rb).  But Rb needs Carbon.h header included in dylibs which are called by Rb programs.

In C, it's easy:

#include "Carbon/Carbon.h"

Unfortunately, I am not familier to C, so that, I want to write the library in FPC.  How it can be achieved in FPC?
Could you give me some suggestions?

I have used CarbonEventsCore.ppu, but found not sufficient enough.  The application still crashes in certain circumstances.
Reply | Threaded
Open this post in threaded view
|

Re: How to include Carbon.h?

Jonas Maebe-2

On 19 apr 2007, at 11:01, BlueCat wrote:

> I'm using FPC 2.0.4 on MacOSX 1.4.9 PPC and trying to write a  
> Pascal library
> that is externally called from REALbasic (Rb).  But Rb needs  
> Carbon.h header
> included in dylibs which are called by Rb programs.
>
> In C, it's easy:
>
> #include "Carbon/Carbon.h"

Including that header that does not add any code to your dynamic  
library, so the actual requirement has to be something else. I know  
someone who is using an FPC-compiled dynamic library in a (universal)  
REALBasic program, so it is definitely possible.

> Unfortunately, I am not familier to C, so that, I want to write the  
> library
> in FPC.  How it can be achieved in FPC?

The closest equivalent is the FPCMacOSAll unit. But this will not add  
any code to your program if you don't call any of its routines,  
similarly to including Carbon/Carbon.h

> Could you give me some suggestions?
>
> I have used CarbonEventsCore.ppu, but found not sufficient enough.  
> The
> application still crashes in certain circumstances.

The crashes are probably due to a bug in the 2.0.4 compiler, not due  
to not including Carbon/Carbon.h or so. Try installing a snapshot:
ftp://ftp.freepascal.org/pub/fpc/snapshot/v21/powerpc-macosx/
fpc-2.1.1.powerpc-macosx.dmg

Further, make sure that all routines which you call from the  
REALBasic program have the calling convention "mwpascal". You can  
switch this calling convention on in all Pascal sources by adding  
this at the top:

{$calling mwpascal}


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to include Carbon.h?

Ingemar Ragnemalm
In reply to this post by BlueCat

> Hello, Pascal folks,
>
> I'm using FPC 2.0.4 on MacOSX 1.4.9 PPC and trying to write a Pascal library
> that is externally called from REALbasic (Rb).  But Rb needs Carbon.h header
> included in dylibs which are called by Rb programs.
>
> In C, it's easy:
>
> #include "Carbon/Carbon.h"
>
> Unfortunately, I am not familier to C, so that, I want to write the library
> in FPC.  How it can be achieved in FPC?
> Could you give me some suggestions?
>
> I have used CarbonEventsCore.ppu, but found not sufficient enough.  The
> application still crashes in certain circumstances.
>  

So the Pascal code compiles, links with the RB code, but crashes when it
runs? How far does it run?

It can't possibly be a problem with calling conventions? Pascal routines
that you need to call from C need to be declared appropriately, with
"cdecl" or "MWPascal" (which are equivalent, I think). Could that be the
problem? Then Jonas' suggestion to use "{$calling mwpascal}" could be
just what you need. Then your calls will be compiled to use C calling
conventions.

Another problem when mixing languages is the Pascal RTL. You might need
to link with that. (That is not necessarily trivial, I am having trouble
with that myself.) But that doesn't sound like your problem, right?


/Ingemar

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to include Carbon.h?

Milan Marusinec
In reply to this post by BlueCat


BlueCat wrote:
> Hello, Pascal folks,
>
> I'm using FPC 2.0.4 on MacOSX 1.4.9 PPC and trying to write a Pascal library
> that is externally called from REALbasic (Rb).  But Rb needs Carbon.h header
> included in dylibs which are called by Rb programs.

To include Carbon.pas in your fpc-mac os X project you need
additional libraries, which are not a part of the Free Pascal on mac.
They are the Universal Pascal Interfaces (UPI), which are a part of the
Apple Universal Interfaces, which can be downloaded at:
 
ftp://ftp.apple.com/developer/Development_Kits/UniversalInterfaces3.4.2.dmg

Also don't forget to pass -k"-framework Carbon" to the fpc compiler on
command line.

Milano

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to include Carbon.h?

BlueCat
In reply to this post by Ingemar Ragnemalm
Ingemar Ragnemalm wrote
So the Pascal code compiles, links with the RB code, but crashes when it
runs? How far does it run?
[snip]
Thanks you, Jonas and Ingmal.  Here is an example.

Library stub;
{$calling mwpascal}
uses CarbonEventsCore;

function aint(i : smallint) : smallint;
begin
  aint := succ(sqr(i))
end;

exports
  aint name '_aint',
  aint;

end.

I compiled this with FPC 2.1.1 (as suggested by Jonas) and got a .dylib file.  This can be called REALbasic(Rb) through Soft Declare statement.  The Rb application runs for some extents.  The function aint is successfuly called. Rb app displays its result correctly.  If you select Quit from application menu, it quits.  But if you open a modal dialog (e.g. that to get a file for opening) from the application and make scrolls, looking around directories etc. , the application crashes.

Of course, crash does not occure before calling function aint.

I have tryed cdecl and stdcall calling conventions, but the results are same.

CarbonEventsCore really does something.  Without CarbonEventsCore, the application won't open a modal dialog but crashes at launching it.

I have considered switching Rb to Lazarus.  But Lazarus currently lacks components to handle MPEG2 or Quicktime movie files.  That's the main reason why I use Rb instead.
Reply | Threaded
Open this post in threaded view
|

Re: Re: How to include Carbon.h?

Jonas Maebe-2
In reply to this post by Milan Marusinec

On 19 apr 2007, at 12:57, Milan Marusinec wrote:

> BlueCat wrote:
>> Hello, Pascal folks,
>> I'm using FPC 2.0.4 on MacOSX 1.4.9 PPC and trying to write a  
>> Pascal library
>> that is externally called from REALbasic (Rb).  But Rb needs  
>> Carbon.h header
>> included in dylibs which are called by Rb programs.
>
> To include Carbon.pas in your fpc-mac os X project you need
> additional libraries, which are not a part of the Free Pascal on mac.
> They are the Universal Pascal Interfaces (UPI), which are a part of  
> the
> Apple Universal Interfaces, which can be downloaded at:
> ftp://ftp.apple.com/developer/Development_Kits/
> UniversalInterfaces3.4.2.dmg

This is incorrect. That version is very outdated and contains several  
errors. As of 2.0.4, FPC contains the common Mac OS interfaces for  
FPC, GPC and MW, which contain many more Pascal headers than what  
Apple distributes and also many bug fixes.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: How to include Carbon.h?

BlueCat
Milan, Jonas, Thanks a lot.

Jonas Maebe-2 wrote
[snip]
This is incorrect. That version is very outdated and contains several  
errors. As of 2.0.4, FPC contains the common Mac OS interfaces for  
FPC, GPC and MW, which contain many more Pascal headers than what  
Apple distributes and also many bug fixes.
Maybe because I am new on Nabble, I did not notice Milan's post before my second post.  Milan, Your suggestiion was very helpful for me so that I can realise how to link Carbon framework in FPC with a command line option -k.  Now, I can use FPCMacOSAll unit, which may Jonas mean, without get the linker angry.  My humble stub comes to be;

Library stub;
{$calling mwpascal}
uses FPCMacOSAll;

function aint(i : smallint) : smallint;
begin
  aint := succ(sqr(i))
end;

exports
  aint name '_aint',
  aint;

end.

compiled with options

fpc -Mmacpas -k"-framework Carbon" smallstub

The dylib now works rather well, but still is somewhat unstable.  The system modal dialog still hangs after heavy usage.  I am now wondering this is due to any initialisation problem.

I have an additional question to Milan.  Google tells me that there is AggPas program by Milan, maybe you :-)  Does this application use QuickTime API?  If QT or MPEG2 file can be easily accessed by FPC, porting my application to Lazarus become quite a favourable choice (but it must be discussed in another thread).

Reply | Threaded
Open this post in threaded view
|

Re: How to include Carbon.h?

Milan Marusinec
BlueCat wrote:
> I have an additional question to Milan.  Google tells me that there is
> AggPas program by Milan, maybe you :-)  Does this application use QuickTime
> API?  If QT or MPEG2 file can be easily accessed by FPC, porting my
> application to Lazarus become quite a favourable choice (but it must be
> discussed in another thread).

Yes I wrote AggPas. It uses QuickTime in fpc on mac for some image
loadings. Looking at the sources of the Apple UPI i see file
"QuickTimeStreaming.p". Maybe there is what you need.
Don't forget to pass -k"-framework Carbon -framework QuickTime".

Milano


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to include Carbon.h?

BlueCat
"Ing. Milan MaruĊĦinec" wrote
[snip]
Yes I wrote AggPas. It uses QuickTime in fpc on mac for some image
loadings. Looking at the sources of the Apple UPI i see file
"QuickTimeStreaming.p". Maybe there is what you need.
Don't forget to pass -k"-framework Carbon -framework QuickTime".
[snip]
Well, that's good news for me.  I will investigate your code to learn how to call QT from FPC.  Thanks a lot.

There is "QuickTimeStreaming.pas" in my /usr/local/share/fpcsrc/packages/extra/univint directory.  Perhaps that is an equivalent of "QuickTimeStreaming.p".  FPCMacOSAll seems to import this too.

Reply | Threaded
Open this post in threaded view
|

Re: How to include Carbon.h?

BlueCat
In reply to this post by Ingemar Ragnemalm
Ingemar Ragnemalm wrote
[snip]
Another problem when mixing languages is the Pascal RTL. You might need
to link with that. (That is not necessarily trivial, I am having trouble
with that myself.) But that doesn't sound like your problem, right?
Thanks to you all, my library is able to use FPCMacOSAll unit that may include a FPC equivalent of Carbon.h.  But there still remains some unstabilities.  So I'd like to rule out problems of RTC initialisatiion problem and unit entry codes.

I have noticed a label "_PASCALMAIN " in assembler source files.  Is this the entry point of RTL initialisation code?

And I have a vague question.  What document is must-read to know such technical details concerning to FPC and Darwin/Carbon as linking frameworks, FPCMacOSAll unit usage, library entry point and so on.  Such details cannot be found pdfs comming with FPC MacOSX packeage.  So I wondered where I must go to see...
Reply | Threaded
Open this post in threaded view
|

Re: How to include Carbon.h?

Jonas Maebe-2

On 21 Apr 2007, at 05:20, BlueCat wrote:

> Thanks to you all, my library is able to use FPCMacOSAll unit that may
> include a FPC equivalent of Carbon.h.  But there still remains some
> unstabilities.  So I'd like to rule out problems of RTC  
> initialisatiion
> problem and unit entry codes.
>
> I have noticed a label "_PASCALMAIN " in assembler source files.  
> Is this
> the entry point of RTL initialisation code?

It's the entry point of the main program or library. It will, a.o.,  
call the RTL initialisation code. In case of a dynamic library, it's  
automatically called by the dynamic linker when loading the library.

> And I have a vague question.  What document is must-read to know such
> technical details concerning to FPC and Darwin/Carbon as linking  
> frameworks,
> FPCMacOSAll unit usage, library entry point and so on.  Such  
> details cannot
> be found pdfs comming with FPC MacOSX packeage.  So I wondered  
> where I must
> go to see...

There is no such documentation at this time. I don't know of anyone  
working on it either.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal