OS X Programming Tips/fr
From Lazarus-ccr
English (en) Français (fr) Japanese (ja)
[edit] Other Interfaces
- Lazarus known issues (things that will never be fixed) - A list of interface compatibility issues
- Win32/64 Interface - The winapi interface for Windows 95/98/Me/2K/XP/Vista, but not CE
- GTK1 Interface - The gtk1 for Unixes, Mac OS X, Windows
- GTK2 Interface - The gtk2 for Unixes, Mac OS X, Windows
- Carbon Interface - The Carbon Interface for Mac OS X
- Qt Interface - The Qt 4 Interface for Unixes, Mac OS X and linux-based PDAs
- Windows CE Interface - For Pocket PC and Smartphones
- fpGUI Interface - A widgetset completely written in Object Pascal
- Cocoa Interface - The Cocoa Interface for Mac OS X
[edit] Platform specific Tips
- OS X Programming Tips - Lazarus installation, useful tools, Unix commands, and more...
- WinCE Programming Tips - Using the telephone API, sending SMSes, and more...
- Windows Programming Tips - Desktop Windows programming tips.
[edit] Interfaces Development Articles
- Carbon interface internals - If you want to help improving the Carbon interface
- Windows CE Development Notes - For Pocket PC and Smartphones
- Adding a new interface - How to add a new widget set interface
- LCL Defines - Choosing the right options to recompile LCL
[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
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
The gtk and gdk-pixbuf libraries must be installed. This can be done either with Fink or with MacPorts. Especially on OS 10.5 Leopard, MacPorts is much easier to handle than Fink.
[edit] 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] 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"
[edit] Note:
OS X is a special case for installation because of Lazarus's current dependency on 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. The new Carbon IDE may eliminate this requirement, but for now it is recommended to install the libraries even if you don't want to use the GTK IDE. Also, remember that steps 1.2 and 1.3 are 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
Try out Lazarus by opening an X11 window and entering:
cd /usr/local/share/lazarus ./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 Lazarus by double clicking it or dropping it on the dock.
[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 rm -fv $bin/ppcppc 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 $share/lazarus rm -r $share/fpcsrc rm -r $share/doc/fpc-2.1.1 rm -r $share/examples/fpc-2.1.1 rm -r $lib/fpc rm -r $receipts/lazarus-*.pkg rm -r $receipts/fpcsrc-*.pkg rm -r $receipts/fpc-*.pkg
Note that this assumes you have a version 2.1.1 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
[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.
GTK widgetset
| Pros | Cons |
|---|---|
| All standard LCL controls working | Ugly; clunky common dialogs; doesn't look like Mac software |
| Lazarus currently compiled with GTK, so widgetset is well tested | Requires 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 feel | Some LCL controls not yet working |
| Qt widgetset also available for other platforms | Requires Qt interface framework to be installed to run app |
| Any effort put into developing Qt widgetset benefits multiple platforms | Expensive 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 |
Carbon widgetset (see the Carbon Interface page)
| Pros | Cons |
|---|---|
| No additional libraries or frameworks to install; uses Carbon framework included with OS X | Some LCL controls not yet working; currently completion is planned for post-1.0 Lazarus |
| Native OS X look and feel | Only relevant to OS X |
[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] Useful tools to download and install
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.
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 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 is a useful maintenance tool for OS X.
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 directory | cd | cd |
| Make a new directory | mkdir | mkdir |
| Delete a directory | rmdir | rmdir |
| List file directory in chronological order with detail | dir /od | ls -ltr |
| Copy a file, preserving its date-time stamp | copy | cp -p |
| Display contents of a text file | type | cat |
| Delete a file | erase | rm |
| Move a file | move | mv |
| Rename a file | ren | mv |
| Find a file | dir /s | find |
| Grep a file | findstr | grep |
| Display differences between two text files | fc | diff |
| Change file attributes | attrib | chmod |
| “Super-user” root authorization | N/A | sudo |
| Create symbolic link to a file or directory | N/A | ln |
| Shrink executable file size | strip (included w/ Free Pascal) | strip |
[edit] Links
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?
