OS X Programming Tips/ko

From Lazarus wiki
Jump to: navigation, search

English (en) | 日本語 (ja) | 한국어 (ko)

그외 인터페이스

각 플랫폼별 팁

인터페이스 개발에 관련된 내용

Lazarus 와 Free Pascal 설치하기

Subversion (SVN)을 통해서 Free Pascal과 Lazarus를 다운받아서 업데이트하지 않으려면, OS X상에서의 설치는 무시해도 좋습니다. 아래에 계속되는 설치 안내문은 Source Forge (안정버전)를 통해서나 Lazarus snapshot 페이지를 통해서 디스크 이미지파일(.dmg확장자)을 사용해서 Free Pascal과 Lazarus를 설치한다고 가정하고 있습니다.

애플 개발툴 설치하기

애플 개발 툴이 먼저 설치되어 있어야 합니다. Free Pascal은 이 중 몇몇 도구를 필요로 합니다. OS X 10.3에서는, OS설치 CD에서 Xcode 도구를 따로 설치할 수 있습니다. OS X 10.4에서는 타이거(OS) 설치 DVD에서 Xcode 도구를 설치할 수 있습니다.

애플 개발툴이 설치되어 있는 지 알아 보려면, /Library/Receipts 를 보세요. 설치되어 있다면, DeveloperTools.pkg라는 이름의 파일을 확인 할 수 있으며, 그 도구 중 하나를 실행할 수도 있습니다. 예를 들어, 터미널을 열고 "ld"라고 입력해 보세요(따옴표는 빼고). 설치되어 있다면, OS X 링커의 사용법이 보일 것입니다.

Free Pascal을 Xcode 통합환경에서 사용하고자 한다면, Free Pascal 설치 시에 gcc 패키지가 필요합니다. OS X 10.4에서는 개발 툴을 설치할 때 gcc4.0.pkg을 설치함으로써 gcc를 개별 설치할 수 있습니다. 디스크 공간이 넉넉하다면, 모든 패키지가 포함된 Xcode 메타패키지(XcodeTools.mpkg)를 설치하면 됩니다.

X11 설치하기

기본 carbon 위젯셋 대신 gtk 위젯셋을 사용하려면, X11이 설치되어 있어야 합니다. OS X 10.3에서는 OS X에 포함된 Xcode 툴 CD에서 따로 X11을 설치할 수 있습니다. OS X 10.4에서는 타이거 설치 DVD에서 옵션으로 X11을 설치할 수 있습니다.

X11이 설치되어 있는 지 확인하려면, /Applications/Utilities 폴더에서 X11이라는 이름의 어플리케이션이 있는 지 확인해 보면 됩니다.

X11을 Dock에 끌어다 놓으면 X11을 한번의 클릭으로 수행시킬 수 있습니다.

gtk 와 gdk-pixbuf 설치하기

기본 carbon 위젯셋 대신 gtk 위젯셋을 사용하고자 한다면, gtkgdk-pixbuf 라이브러리가 설치되어 있어야 합니다. Fink나 MacPorts로 설치할 수 있습니다.

Fink로 gtk 설치하기

OS X 버전(10.3 팬서, 10.4 타이거, 10.5 레오파드)에 맞게 fink의 정확한 버전을 다운 받아서 설치하십시오. 그러면 Fink로 Lazarus에 필요한 GTK와 관련된 패키지를 다운 받아서 설치할 수 있습니다. fink를 설치했다면 먼저 터미널을 열고 다음 명령을 모두 입력하십시오:

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

GTK 라이브러리를 다운받으려고 할 때 창이 뜨면, 암호를 입력하시기 바랍니다. 만일 전화 모뎀을 통해 인터넷에 연결되어 있다면 한 시간은 족히 걸릴 수 있다는 것을 염두에 두시기 바랍니다.

MacPorts로 gtk 설치하기

MacPorts로 라이브러리를 설치하고자 한다면 다음의 절차를 따르십시오. www.macports.org에서 MacPorts를 설치하십시오. MacPorts설치가 완료되었다면 다음 명령어를 입력해서 두 개의 라이브러리를 설치하십시오:

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

Lazrus를 실행하기 전에 아래 명령어를 입력해서 Lazarus에서 라이브러리를 찾을 수 있도록 설정하십시오. 다른 방법으로 Lazarus를 쉘에서 실행하기 전에 bash 시작 스크립트(/etc/profile or ~/.bash_profile)에 추가할 수도 있습니다:

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

만일 작동하지 않는다면, 간단히 다음과 같이 해보십시오.

sudo ln -s /opt/local /sw

노트:

Lazarus 0.9.25부터는 OS X상에서는 carbon 인터페이스를 기본으로 합니다. 따라서 Lazarus나 Lazarus 어플리케이션에서는 더이상 GTK나 X11이 필요하지 않습니다. 아직 carbon 인터페이스에서 동작하지 않는 어플리케이션만 gtk를 필요로 합니다. gtk 위젯셋은 OS X상에서의 GUI를 위해서 X 윈도우와 Gimp 툴킷(GTK)라이브러리가 필요합니다. GTK 라이브러리는 OS X에 기본으로 설치되지 않으므로, 사용자가 직접 설치해야만 합니다. 또한, Lazarus로 개발한 GTK GUI를 사용하는 어플리케이션은 실행시에도 GTK와 X11이 필요하다는 것을 기억해야 합니다. carbon 어플리케이션은 별도의 추가 설치가 필요치 않습니다.

팁:

라이브러리나 콘솔 어플리케이션을 프리 파스칼로 개발하려면, 텍스트 편집기를 사용하고 터미널이나 스크립트 파일을 통해서 컴파일 할 수도 있습니다. 즉, X11이나 fink, GTK 라이브러리, Lazarus를 설치할 필요가 없습니다. 단지 필요한 경우에만 애플 개발툴과 프리 파스칼을 설치하면 됩니다.

Free Pascal과 Lazarus 다운받기

Source ForgeLazarus daily snapshot page를 통해서 모든 세개의 .dmg 파일을 다운받고 설치하십시오

 fpc.dmg
 fpcsrc.dmg
 lazarus.dmg

설치가 끝나면 모든 .dmg파일은 삭제하셔도 괜찮습니다.

Lazarus 실행하기

패키지로 설치했다면 설치한 폴더(/usr/local/share/lazarus)에서 Lazarus아이콘을 더블클릭만 하면 됩니다.

또한 터미널에서 다음을 입력하면 carbon 통합환경이 실행됩니다:

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

노트: 리눅스나 윈도즈에서 처럼 실행 디렉토리에서 실행시키지 마십시오. OS X에서는 lazarus.app 디렉토리에서 실행시켜야 합니다.

gtk버전으로 컴파일했다면 X11을 열고 다음을 입력하십시오:

cd /usr/local/share/lazarus  // 혹은 설치한 경로
./lazarus

Lazarus가 Free Pascal 컴파일러를 찾을 수 없다고 불평하면, Lazarus 통합환경의 Environment | Environment options 에서 Compiler Path 항목에 /usr/local/bin/ppcppc 를 입력하십시오(인텔맥이라면 /usr/local/bin/ppc386).

팁:

GTK Lazarus를 더블클릭으로 실행하는 방법이나 Dock에 추가하는 방법에 관한 정보는 아래에 있는 항목들을 참고하십시오.

Lazarus IDE Shortcuts을 참고하십시오.

X11 팁

  • Lazarus에서의 마우스 클릭을 원하는 대로 수정하려면, X11창에서 다음을 입력하세요:
 defaults write com.apple.x11 wm_click_through -bool true

그런다음 X11을 내리고 변경내용을 적용시키기 위해서 재시동 하십시오.

  • X11을 내릴 때마다 성가시게 뜨는 확인창을 제거하려면, X11창에서 다음을 입력하세요. 그러면 X11이 새로 시작합니다:
 defaults write com.apple.x11 no_quit_alert true
  • 간단하게 Lazarus를 시작하려면, X11상태에서 Applications | Customize 메뉴를 선택한 다음, 항목을 하나 추가한다. 이름(Name)에 Lazarus라고 입력하고, 명령어(Command)에 다음을 입력한다:
 open -a /usr/local/share/lazarus/lazarus

완료(Done)을 선택하면 창이 닫힌다. 이제 X11메뉴에서 Application | Lazarus 를 선택하기만 하면 바로 Lazarus를 띄울 수 있다.

Qt 인터페이스

QT 위젯셋과 Lazarus를 같이 사용하는것에 흥미가 있다면 Qt interface framework을 설치하세요. Qt Interface Mac 페이지에 OS X에서 사용하기 위한 정보가 있습니다.

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

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 X Some LCL controls not yet working
Native OS X look and feel Only relevant to 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

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

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.

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.

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).

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


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?