OS X Programming Tips

From Lazarus-ccr

Jump to: navigation, search

English (en) Français (fr) 日本語 (ja) 한국어 (ko)

Contents

[edit] Other Interfaces

[edit] Platform specific Tips

[edit] Interfaces Development Articles

[edit] Installing Lazarus and Free Pascal

If you won't be using Subversion (SVN) to download and update the Free Pascal and Lazarus source code, you can ignore instructions elsewhere about installation on OS X. The following instructions assume you will only be installing Free Pascal and Lazarus using the disk image (.dmg extension) files available from Source Forge (stable version) or the Lazarus snapshot page.

[edit] Installing the Apple Developer Tools

Make sure the Apple Developer Tools are installed. Some of these tools are needed by Free Pascal. With OS X 10.3, they can be installed from the separate Xcode Tools CD that came with OS X. With OS X 10.4, they can be installed from the Xcode Tools folder on the Tiger Install DVD.

How to you know whether the Apple Developer Tools are installed? Look in /Library/Receipts. There should be a file named DeveloperTools.pkg if the tools are installed. You can also try running one of the tools. For example, open a Terminal window and enter "ld" (without quotes). If installed, the OS X linker will display its usage syntax.

[edit] Tip

The Free Pascal installer also requires that the gcc package be installed in case you should want to use Free Pascal with the Xcode IDE. With OS X 10.4, gcc is separate from the Developer Tools so be sure to install gcc4.0.pkg, too. If you have plenty of disk space, you can just install the Xcode metapackage (XcodeTools.mpkg), which installs all packages.

[edit] Installing X11

If you want to use the gtk widgetset instead of the default carbon widgetset, then make sure X11 is installed. With OS X 10.3, X11 can be installed from the separate Xcode Tools CD that came with OS X. With OS X 10.4, X11 can be installed from the Optional Installs folder on the Tiger Install DVD.

How do you know whether X11 installed? Look in the /Applications/Utilities folder for an app named X11.

[edit] Tip

Drag and drop X11 onto the dock so you'll have a one-click way of opening an X11 window.

[edit] Installing gtk and gdk-pixbuf

If you want to use the gtk widgetset instead of the default carbon widgetset the gtk and gdk-pixbuf libraries must be installed. This can be done either with Fink or with MacPorts.

[edit] Installing gtk using Fink

Download and install the correct version of fink for your version of OS X (10.3 Panther, 10.4 Tiger or 10.5 Leopard) and then use it to download and install the additional GTK and related packages required by Lazarus. Once fink is installed, open a Terminal window and enter all on one line:

 sudo /sw/bin/apt-get install gtk+ gdk-pixbuf

Enter your password when prompted to start downloading the binary GTK libraries. Note that if your Internet connection is via a phone modem, downloading may take up to an hour.

[edit] Installing gtk using MacPorts

Follow these steps to install the libraries using MacPorts. Install MacPorts from www.macports.org. When MacPorts is set up, you need to install two library sets using the following commands:

sudo /opt/local/bin/port install gtk1
sudo /opt/local/bin/port install gdk-pixbuf

Then make sure that Lazarus can find the library by using the command below before running Lazarus. Alternatively you can add this command to your bash startup script (/etc/profile or ~/.bash_profile) before you start the shell to run Lazarus:

export DYLD_FALLBACK_LIBRARY_PATH="/opt/local/lib:$DYLD_FALLBACK_LIBRARY_PATH"

If this doesn't work, simply do

sudo ln -s /opt/local /sw

[edit] Note:

Since 0.9.25 Lazarus uses the carbon interface as default under OS X. So you don't need GTK or X11 anymore to run Lazarus or Lazarus applications. You only need gtk if your application does not work with the carbon interface yet. The gtk widgetset requires X Windows and the Gimp Toolkit (GTK) libraries for its GUI on OS X. The GTK libraries are not installed by default on OS X so you have to install them yourself. Also, remember that GTK and X11 will also be required for OS X users of GTK GUI apps you develop with Lazarus. The installation is not required for Carbon applications that you make.

[edit] Tip:

If you only need to develop libraries or console apps with Free Pascal, you can use any text editor and compile from a Terminal window or script file, meaning you don't have to install X11, fink, the GTK libraries, or Lazarus. Only the Apple Developer Tools and Free Pascal need to be installed in that case.

[edit] Download Free Pascal and Lazarus

Download and install all three .dmg files from Source Forge or the Lazarus daily snapshot page:

 fpc.dmg
 fpcsrc.dmg
 lazarus.dmg

When done installing, you can delete these .dmg files.

[edit] Start Lazarus

If you installed via package: Just double-click on the lazarus icon in the install folder (/usr/local/share/lazarus).

You can also start the carbon IDE from a terminal with:

cd /usr/local/share/lazarus
open lazarus.app 

Note: Do not start the lazarus executable directly (as under linux and windows). OS X needs the lazarus.app directory.

If you compiled the gtk version open an X11 window and enter:

cd /usr/local/share/lazarus  // or whatever path you chose
./lazarus

If Lazarus complains about not being able to find the Free Pascal compiler, choose Environment | Environment options in the Lazarus IDE and enter /usr/local/bin/ppcppc in the Compiler path box (/usr/local/bin/ppc386 on Intel Macs).

[edit] Tip:

See the section below on creating an app bundle for information on how to start a GTK Lazarus by double clicking it or dropping it on the dock.

See Lazarus IDE Shortcuts.

[edit] X11 tips

  • To make Lazarus mouse clicks behave more like you would expect them to, enter the following in an X11 window:
 defaults write com.apple.x11 wm_click_through -bool true

Then shut down X11 and restart it to make this change take effect.

  • To eliminate the annoying confirmation dialog that's displayed each time you shut down X11, enter the following in an X11 window, then restart X11:
 defaults write com.apple.x11 no_quit_alert true
  • To simplify starting Lazarus, choose Applications | Customize in X11, then click Add Item. Under Name enter Lazarus and under Command enter the following:
 open -a /usr/local/share/lazarus/lazarus

Click Done to close. Now you can start Lazarus from X11 simply by choosing Applications | Lazarus.

[edit] Qt Interface

If you have any interest in using the Qt widgetset with Lazarus, install the Qt interface framework. See the Qt Interface Mac page for complete details on using it with OS X.

[edit] Uninstalling Lazarus and Free Pascal

Normally you uninstall an application on OS X simply by dragging it from the Applications folder to the trash. But because Lazarus and Free Pascal are development tools, they're installed in several folders that you don't normally see in Finder.

You can copy and save the commands below to file uninstallLaz.sh and run it if you need to uninstall Lazarus and Free Pascal. You can usually install newer versions of Lazarus and Free Pascal over older versions, but as with most software it's not a bad idea to clean out everything before you install a newer version. This list of commands should also give you a sense of where the various pieces of Lazarus and Free Pascal are located.

bin=/usr/local/bin
share=/usr/local/share
lib=/usr/local/lib
receipts=/Library/Receipts
private=/private/etc

rm -fv $bin/ppcppc
rm -fv $bin/ppc386
rm -fv $bin/bin2obj
rm -fv $bin/data2inc
rm -fv $bin/delp
rm -fv $bin/fd2pascal
rm -fv $bin/fpc
rm -fv $bin/fpcmake
rm -fv $bin/fpcmkcfg
rm -fv $bin/fpcsubst
rm -fv $bin/fpdoc
rm -fv $bin/fprcp
rm -fv $bin/h2pas
rm -fv $bin/h2paspp
rm -fv $bin/makeskel
rm -fv $bin/mkxmlrpc
rm -fv $bin/plex
rm -fv $bin/postw32
rm -fv $bin/ppdep
rm -fv $bin/ppudump
rm -fv $bin/ppufiles
rm -fv $bin/ppumove
rm -fv $bin/ptop
rm -fv $bin/pyacc
rm -fv $bin/rstconv
rm -fv $bin/unitdiff
rm -r $private/lazarus
rm -r $bin/lazarus
rm -r $share/lazarus
rm -r $share/fpcsrc
rm -r $share/doc/fpc-2.2.2
rm -r $share/examples/fpc-2.2.2

rm -r $lib/fpc

rm -r $receipts/lazarus-*.pkg
rm -r $receipts/fpcsrc-*.pkg
rm -r $receipts/fpc-*.pkg

rm -fv /etc/fpc.cfg

Note that this assumes you have version 2.2.2 snapshot of Free Pascal installed. If you have a different version, change the two relevant commands to specify your version.

To run this script, change to the directory where it's stored and enter:

chmod +x uninstallLaz.sh
sudo ./uninstallLaz.sh

Then enter your password when prompted.

Note: Lazarus also creates a .lazarus folder in your home directory where it stores environment settings and a list of recently opened projects and files. You can leave this folder alone if you want the new version of Lazarus you're installing to use your old settings. OS X normally doesn't show files or folders whose names start with a dot (.). To see this folder and its contents, enter the following in a terminal window:

cd ~/
ls -al
cd .lazarus
ls

To remove it, execute the following commands:

cd ~/
rm -rv .lazarus

[edit] Choice of Lazarus LCL widgetsets

With OS X you have a choice of three widgetsets to use with Lazarus. Each has its strengths and weaknesses. Two are still under development and are useful mostly for seeing what the future holds for Lazarus on OS X.

Carbon widgetset (see the Carbon Interface page)

Pros Cons
No additional libraries or frameworks to install; uses Carbon framework included with OS XSome LCL controls not yet working
Native OS X look and feelOnly relevant to OS X

GTK widgetset

Pros Cons
All standard LCL controls workingUgly; clunky common dialogs; doesn't look like Mac software
Lazarus currently compiled with GTK, so widgetset is well testedRequires X11 and bulky GTK to be installed to run Lazarus or any GUI apps created with Lazarus
Have to start Lazarus at command line -- can't double-click Lazarus or place on dock (same with GUI apps) -- although see next topic for how you can add this capability yourself


Qt widgetset (see the Qt Interface Mac page)

Pros Cons
Native OS X look and feelSome LCL controls not yet working
Qt widgetset also available for other platformsRequires Qt interface framework to be installed to run app
Any effort put into developing Qt widgetset benefits multiple platformsExpensive commercial license required to use Qt libraries with apps that are not open source
Qt interface provides rich functionality
Qt widgetset is easier to develop than other widgetsets

[edit] Creating an app bundle for a GTK application (including Lazarus)

With OS X, you need to create an app bundle for an executable file in order to drop it on the dock or launch it by double-clicking. An app bundle is really just a special folder that can have the same name as the executable, but with an .app extension. Finder doesn't display the .app extension, although you'll see it if you use ls in a Terminal window.

The Carbon and Qt pages both explain how to create an app bundle for apps compiled with those widgetsets. With a GTK app, it's a bit more complicated since X11 always has to be running in order to run the app. Copy and paste the following script to file create_gtk_app.sh, use chmod +x on the file so OS X will let you run it, then run it as follows to create an app bundle (substitute the name of your own GUI app compiled with the GTK widgetset):

 ./create_gtk_app.sh execfile

You can even use this script to create an app bundle for Lazarus itself, since it's a GTK app. Just change to wherever you want to create the app bundle (for example, the Applications folder) and run the script:

 [path_to_script]/create_gtk_app.sh lazarus

Now you can drag and drop the resulting Lazarus app bundle onto the dock just like any other application.

For more information, study the script.

Tip: In the script below, one of the lines is really long and may get chopped off when you print this page. Here's the line on two lines. Just be sure it's all one line in your script file.

 echo '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" 
  "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >>$plistfile
#!/bin/sh
# Force Bourne shell in case tcsh is default.
#
appname=$1
appfolder=$appname.app
macosfolder=$appfolder/Contents/MacOS
plistfile=$appfolder/Contents/Info.plist
if [ $appname = "lazarus" ]
then
  appfile=/usr/local/share/lazarus/lazarus
else
  appfile=$appname
fi
#
if [ "$appname" = "" ]
then
  echo "Usage: $0 executable_file"
  echo "Creates .app bundle (folder) for executable file that uses GTK widgetset"
elif ! [ -e $appfile ]
then
  echo "$appfile does not exist"
elif [ -e $appfolder ]
then
  echo "$appfolder already exists"
else
  echo "Creating $appfolder..."
  mkdir $appfolder
  mkdir $appfolder/Contents
  mkdir $appfolder/Contents/MacOS
  mkdir $appfolder/Contents/Resources
#
  if [ $appname = "lazarus" ]
  then
# This is special case for lazarus IDE.
# Create a script file in .app folder that starts X11, then Lazarus.
    echo '#!/bin/sh' >$macosfolder/$appname.sh
    echo '# This script starts X11, then Lazarus.' >>$macosfolder/$appname.sh
    echo 'open -a x11' >>$macosfolder/$appname.sh
    echo "export DISPLAY=':0.0'" >>$macosfolder/$appname.sh
    echo 'open -a '$appfile >>$macosfolder/$appname.sh                #IMPORTANT! If you're running TCSH, or other,
    chmod +x $macosfolder/$appname.sh                                 # "open -a" doesn't work. Use "open" instead
  else
# Instead of copying executable into .app folder after each compile,
# simply create a symbolic link to executable.
    ln -s ../../../$appname $macosfolder/$appname
# Create a little script file in .app folder that opens executable with X11.
    echo '#!/bin/sh' >$macosfolder/$appname.sh
    echo '# This script opens the executable file with X11.' >>$macosfolder/$appname.sh
    echo 'open ${0/%.sh}' >>$macosfolder/$appname.sh
    chmod +x $macosfolder/$appname.sh
  fi
#
# Create PkgInfo file.
  echo "APPL????" >$appfolder/Contents/PkgInfo
#
# Create information property list file (Info.plist).
  echo '<?xml version="1.0" encoding="UTF-8"?>' >$plistfile
  echo '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >>$plistfile
  echo '<plist version="1.0">' >>$plistfile
  echo '<dict>' >>$plistfile
  echo '  <key>CFBundleDevelopmentRegion</key>' >>$plistfile
  echo '  <string>English</string>' >>$plistfile
  echo '  <key>CFBundleExecutable</key>' >>$plistfile
  echo '  <string>'$appname'.sh</string>' >>$plistfile
  echo '  <key>CFBundleInfoDictionaryVersion</key>' >>$plistfile
  echo '  <string>6.0</string>' >>$plistfile
  echo '  <key>CFBundlePackageType</key>' >>$plistfile
  echo '  <string>APPL</string>' >>$plistfile
  echo '  <key>CFBundleSignature</key>' >>$plistfile
  echo '  <string>????</string>' >>$plistfile
  echo '  <key>CFBundleVersion</key>' >>$plistfile
  echo '  <string>1.0</string>' >>$plistfile
  echo '  <key>CSResourcesFileMapped</key>' >>$plistfile
  echo '  <true/>' >>$plistfile
  echo '</dict>' >>$plistfile
  echo '</plist>' >>$plistfile
fi

[edit] Adding an icon to your app bundle

5.1 Use OS X's Icon Composer or a similar program to create your app's icon file (.icns extension).

5.2 Copy the .icns file to your app bundle's Resources folder. For example, from the command line:

 cp -p myapp.icns myapp.app/contents/resources

5.3 Add the following key to your app bundle's Info.plist file:

 <key>CFBundleIconFile</key>
 <string>myapp.icns</string>

5.4 Sometimes Finder doesn't "refresh" an app to use the new icon, continuing instead to show the default icon. You can try these things to force Finder to use the new icon:

  • Log out and back in again.
  • Drag (move) your application to a different folder, then back again.
  • Restart Finder (Esc-Option-Apple keys).

You shouldn't have this problem with Finder when you install your app on other computers. Finder apparently only has trouble when you change the icon from what it was when the app was first created.

[edit] Compiling under 10.5 for 10.4 and below

When compiling an application on Mac OS X without any special options, the application is only guaranteed to work on that particular major Mac OS X release (e.g., when compiling under Mac OS X 10.5.6, the application is only guaranteed to work on Mac OS X 10.5.x).

Here is an example of an error that you may encounter when running an application compiled for Mac OS X 10.5 under Mac OS X 10.4 in case you use the widestring manager:

dyld: Library not loaded: /usr/lib/libiconv.2.dylib
  Referenced from: /Volumes/..../yourprogram
  Reason: Incompatible library version: yourprogram requires version 7.0.0 or later, but libiconv.2.dylib provides version 5.0.0
Trace/BPT trap

To make sure that it runs under previous Mac OS X releases, use the -macosx_version_min linker parameter and link against the appropriate SDK (e.g. -XR/Developer/SDKs/MacOSX10.4u.sdk/). For example, to compile an application that should work on Mac OS X 10.4 and higher:

Add to Project / Compiler options / Other / Custom options:

-k-macosx_version_min -k10.4
-XR/Developer/SDKs/MacOSX10.4u.sdk/

Note: The path /Developer depends on where you installed the Apple developer tools, and may be different if you chose a different location. Do not assume it will always be /Developer.

[edit] Useful tools to download and install

TextWrangler

Every programmer needs a great text editor, but OS X's TextEdit app is not it. Instead, download and install TextWrangler, a freeware text editor that can highlight Pascal syntax, even highlight script file syntax.

Other editors include AlphaX, BBedit, or you can use OS X's Xcode IDE to edit text files.

Sogudi

OS X's man pages (manual pages) are scandalously hard to use and indicate OS X's Unix heritage. Sogudi is a little add-on for OS X's Safari Web browser. Once installed, you can type "man:" in Safari's URL box, followed by the name of the command whose manual you want to view. For example, to view the manual for the grep command, enter the following:

 man:grep

Now you can scroll up and down in the manual, print it, even search it just like you do a Web page.

NeoOffice

NeoOffice is an OS X version of OpenOffice.org that actually looks and behaves like an OS X app. Once you've started using Neo you'll never want to use the plain X11 version of OO again.

Unix and Open Source downloads

Apple provides downloads of numerous free applications, many of which have been ported from Unix and run under X11, such as the venerable Gimp.

Onyx

Onyx is a useful maintenance tool for OS X.

Cyberduck

Need an FTP client app for OS X? Cyberduck is an open source FTP client specifically designed for OS X.

[edit] Useful commands and tools included with OS X

open

Use open to start an application or open a file from the command line. This is useful when you’re working in a terminal window and don’t want to fire up the app and navigate to where you’re working just to open a file there. For example, to open a Pascal file in the current directory in TextWrangler, enter the following:

 open –a textwrangler myfile.pas

zip / unzip

These standard command-line programs are installed with OS X’s Xcode tools (on a separate CD with OS X 10.3).

Console

Drag this app from /Applications/Utilities and drop it on the dock so you always have it handy. Launch it whenever you want to see messages or errors outputted to the console by GUI apps (for example, when they crash). Invaluable for debugging.

Activity Monitor

This app is also in /Applications/Utilities and is useful for monitoring CPU and disk usage.

otool / install_name_tool

Use otool to display information about an executable file or library. For example, enter the following to see information about Lazarus:

 cd /usr/local/share/lazarus
 otool –L lazarus

This shows that Lazarus is dependent on various libraries in /sw/lib, /usr/X11R6/lib and /usr/lib, as you would expect.

Use install_name_tool with the –change switch to change where an executable file or library looks for a library that it requires.

Grab

Use Grab (in /Applications/Utilities) to create a screenshot or window shot, then save it to a disk file.

Icon Composer

Use Icon Composer (in /Developer/Applications/Utilities) to create icon files (.icns) for use with your .app bundles.

PackageMaker / Disk Utility

Use these apps to create disk image (.dmg) files for deploying your apps. See Deploying Your Application for more information.

PackageMaker is in /Developer/Applications/Utilities. Disk Utility is in /Applications/Utilities. You can drag and drop both of them on the dock.

Script Editor / osascript

Use Script Editor to edit, compile and run AppleScript files. It’s located in /Applications/AppleScript.

Use osascript to execute an AppleScript command or file from the command line or from a script file. For more information, enter man:osascript in Safari once you have Sogudi installed (see above).

[edit] Commonly used Unix commands

If you’re coming to OS X from Windows, you may find some of its Unix terminal commands confusing. Here are some equivalents. For more information about a command, enter man:command in Safari once you have Sogudi installed (see above).

Action Windows command prompt window OS X Terminal or X11 window
Change to a different directorycdcd
Make a new directorymkdirmkdir
Delete a directoryrmdirrmdir
List file directory in chronological order with detaildir /odls -ltr
Copy a file, preserving its date-time stampcopycp -p
Display contents of a text filetypecat
Delete a fileeraserm
Move a filemovemv
Rename a filerenmv
Find a filedir /sfind
Grep a filefindstrgrep
Display differences between two text filesfcdiff
Change file attributesattribchmod
“Super-user” root authorizationN/Asudo
Create symbolic link to a file or directoryN/Aln
Shrink executable file sizestrip (included w/ Free Pascal)strip


[edit] Links

Apple developer documents

Coming to OS X from Unix or Linux?

Coming to OS X from Windows and want to learn Unix scripting?

Coming to Object Pascal from C++, C# or Java?