Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-04

How to include Carbon.h?

11 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

View this message in context: http://www.nabble.com/How-to-include-Carbon.h--tf3605888.html#a10074354 Sent from the Free Pascal - General mailing list .

Reply by email

Jonas Maebe
to BlueCat
BlueCat wrote 9 lines
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

Show quoted text 5 lines
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

Reply by email

Ingemar Ragnemalm
Show quoted text 17 lines
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

Reply by email

Milan Marusinec
to BlueCat: I'm using FPC 2.0.4 on MacOSX 1.4.9 PPC and trying to write a Pascal library that is…
BlueCat wrote 5 lines
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

Reply by email

BlueCat
to Ingemar Ragnemalm: So the Pascal code compiles, links with the RB code, but crashes when it runs? How far…
Ingemar Ragnemalm wrote 7 lines
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.

View this message in context: http://www.nabble.com/How-to-include-Carbon.h--tf3605888.html#a10091628 Sent from the Free Pascal - General mailing list .

Reply by email

Jonas Maebe
to Milan Marusinec: To include Carbon.pas in your fpc-mac os X project you need additional libraries, which…
Milan Marusinec wrote 15 lines
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

Reply by email

BlueCat
to Jonas Maebe

Milan, Jonas, Thanks a lot.

Jonas Maebe-2 wrote 7 lines
[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).

View this message in context: http://www.nabble.com/How-to-include-Carbon.h--tf3605888.html#a10094672 Sent from the Free Pascal - General mailing list .

Reply by email

Milan Marusinec
to BlueCat
BlueCat wrote 5 lines
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

Reply by email

BlueCat
to Milan Marusinec
"Ing. Milan Marušinec" wrote 8 lines
[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.

View this message in context: http://www.nabble.com/How-to-include-Carbon.h--tf3605888.html#a10113233 Sent from the Free Pascal - General mailing list .

Reply by email

BlueCat
to Ingemar Ragnemalm: So the Pascal code compiles, links with the RB code, but crashes when it runs? How far…
Ingemar Ragnemalm wrote 6 lines
[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...

View this message in context: http://www.nabble.com/How-to-include-Carbon.h--tf3605888.html#a10113330 Sent from the Free Pascal - General mailing list .

Reply by email

Jonas Maebe
to BlueCat
BlueCat wrote 9 lines
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.

Show quoted text 8 lines
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

Reply by email