Difference between revisions of "Console Mode Pascal/ja"

From Lazarus wiki
Jump to navigationJump to search
Line 71: Line 71:
 
== IDEで実行する ==
 
== IDEで実行する ==
  
From the Main Menu, select Run -> Run Parameters, then check the box for "Use launching application". The first time you do this and try the Compile/Run sequence, you will probably get a rude message to say
+
メインメニューから、Run->Run Parametersを選択し、"Use launching application"にチェックをつけます。
 +
初回これを実行して、コンパイル/実行の手順をふむと、次のようなメッセージが表示されるかもしれません。
  
 
  "xterm: Can't execvp /usr/share/lazarus//tools/runwait.sh: Permission denied".   
 
  "xterm: Can't execvp /usr/share/lazarus//tools/runwait.sh: Permission denied".   
  
If this happens, you need to change the permissions on the appropriate file (for example using chmod +x filename, or using the Windows utility for changing permissions); you might have to do this as root. After this, each time you launch your program, a console box will appear and all your text i/o (readln, writeln etc) will appear in it.
+
このようになったら、ファイルに適切なパーミッションを付加します。(たとえば、chmod +x ファイル名 などとします)。
   
+
これらの作業はルートとして実行しなくてはなりません。この後でプログラムを実行する度に、コンソールウインドウが表示されて、readln,writelnのようなテキスト入出力が実行されます。
After your program has finished execution, a message "Press enter" appears on the screen. Thus any output your program generated will remain on the screen until you have had a chance to read it; after you press 'enter' the console window closes.
+
 
 +
プログラムの実行終了後、"Press enter"メッセージが画面上にあらわれます。これは、プログラムの最後の出力メッセージを読める機会をあたえてくれます。enterキーを押すと、コンソールウインドウが閉じます。
  
 
== 例題 ==
 
== 例題 ==

Revision as of 07:12, 3 January 2009

Deutsch (de) English (en) español (es) suomi (fi) magyar (hu) 日本語 (ja) русский (ru) slovenčina (sk)

日本語版メニュー
メインページ - Lazarus Documentation日本語版 - 翻訳ノート - 日本語障害情報

コンソールモードPascalプログラミング

User:Kirkpatcによる

今日、多くのPascalプログラマにとって、統合環境(IDE)でGUIプログラミングをするのが流行っています。また、Pascal言語の基本的なツールを書きたい、というビギナーの方も多いでしょう。 その一方、複雑なシステム制御のために、コンソールやテキストモードでアプリケーションを書きたいと思っているかもしれません。

LazarusはPascalを勉強するための理想的な環境であり、テキストモードプログラムを開発することができます。ソースハイライト付きのエディタ、ライブラリアクセス、複雑な検索や、コード補完、文法チェックなど、全てのIDEの機能が使えます。ビジュアルコンポーネントのフォームが必要ないものでも、Lazarusのエディタは偉大なプログラム開発環境です。 コンパイルやデバッグ実行などの開発サイクルの間、エディタを離れる必要はありません。

コンソールモードのプログラムを開始するには、メインメニューで Project -> New Projectを選択します。それから、'Program, Console Application または Custom Programを選択します。 IDEはGUIアプリケーションのときに必要な余分なファイルを生成せず、Object Inspectorも表示しませんが、ソースエディタには、プログラムのスケルトンがすでに開かれていて、あなたの入力を待っています。

カスタムプログラム

とても短い最小のpascalプログラムを作る時に選択します。 もし、自分自身ですべてコーディングしたいとき、簡単なテストを行ってすぐファイルを消しそうな、そういう時に利用できます。

プログラム

だいたいカスタムプログラムと同じですが、IDEがほんの少しだけ補助をします。たとえば、他のユニットを追加したときに、IDEは自動的にユニット名をプログラムのusesセクションに追加します。 この動作はプロジェクトオプションで定義されていますので、いつでも'プログラム'と'カスタムプログラム'の動作を切り替えることができます。

まったくのビギナーへの例:

program Project1;
{$mode objfpc}{$H+}
uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { このあとにユニットを追加できます。 };
begin
  writeln('Hello World!');
  readln;
end.

コンソールアプリケーション

新しいクラスをTCustomApplicationから派生させて、コンソールプログラムを作る方法を紹介しましょう。TCustomApplicationは多くの共通の機能、コマンドラインのユーティリティなどの製作を補助する機能を提供しています。 たとえば、コマンドラインオプションをチェックしたり、ヘルプを書いたり、環境変数を調べたり、例外ハンドラを扱えるようにしたりします。すべてのLCLプログラムはこのクラスを自動的に使います。

コンパイル

プログラムの編集が終わったら、メインメニューでRun->Runを選択するか、ボタンバーの緑色の三角のシンボル(実行)をクリックすることで、コンパイルして実行することができます。 すべてのコンパイラメッセージはメッセージボックスに現れます。深刻なエラーがなければ、最終的には、次のように表示されるでしょう。

'Project "Project1" successfully built.:)'.

実行

コンパイルしたプログラムはどこにあるのでしょうか?

まだソースコードを保存してなければ、IDEは一時ディレクトリに保存しています。

Linuxでは /tmp, Windowsでは C:\temp です。詳しくはテストプロジェクトをビルドしたときの Enviroment Options/ Files /Directoryを見てください。

もし、すでのプロジェクトを保存していれば、プログラムはproject1.lpiといったプロジェクトファイルと同じディレクトリに作成されています。

プログラムはコンソールウインドウ、もしくは端末画面から、cdコマンドで上記のディレクトリに移り、プログラムの名前をタイプします。 (UnixやLinuxでは次のように入力してください。

./Project1

) しかし、Lazarusエディタと端末画面を行き来することはあまり便利の良いものではありません。幸い、LazarusにはLazarus環境の下で端末画面を開くメカニズムがあります。

IDEで実行する

メインメニューから、Run->Run Parametersを選択し、"Use launching application"にチェックをつけます。 初回これを実行して、コンパイル/実行の手順をふむと、次のようなメッセージが表示されるかもしれません。

"xterm: Can't execvp /usr/share/lazarus//tools/runwait.sh: Permission denied".  

このようになったら、ファイルに適切なパーミッションを付加します。(たとえば、chmod +x ファイル名 などとします)。 これらの作業はルートとして実行しなくてはなりません。この後でプログラムを実行する度に、コンソールウインドウが表示されて、readln,writelnのようなテキスト入出力が実行されます。

プログラムの実行終了後、"Press enter"メッセージが画面上にあらわれます。これは、プログラムの最後の出力メッセージを読める機会をあたえてくれます。enterキーを押すと、コンソールウインドウが閉じます。

例題

You can use the Lazarus editor to try out all the examples in the standard Pascal text-books, or you can write your own. Some of the most useful procedures are those for executing system commands or for running other programs (which may have been written in Pascal, C or Perl, or may be shell or Batch scripts).

シェルコマンドの実行

Here is an example for a Custom Program or Program:

Program TryShell;
uses classes, unix;
var S: longint;
begin
  S := fpsystem ('/bin/ls -la *.p*'); //lists .pp, .pas, .php, .png etc in current directory
  writeln ('Program exited with status : ', S)
end.

Example: fpcとlazarusを最新にする

Rather more complex commands can be executed. For example, if you have already checked out the SVN repositories for FPC and Lazarus (see fpc buildfaq) you could keep your FPC and Lazarus source files up-to-date by retrieval from the SVN repository with the following sequence of calls:

Program LazUpdate;
uses classes, unix;
var s : longint;
begin 
  S := fpsystem ('cd /usr/local/src/fpc/devel/fpc ; make clean');
  S := fpsystem ('cd /usr/local/src/fpc/devel/lazarus ; make clean');
  S := fpsystem ('cd /usr/local/src/fpc/devel ; svn update fpc >& ~/cvscheckout.log');
  S := fpsystem ('cd /usr/local/src/fpc/devel ; svn update lazarus >& ~/cvslaz.log');
end.

Note that issuing the command

fpsystem ('cd /somedirectory')

followed by

fpsystem ('do something in that subdirectory')

doesn't work, because after each fpsystem function call the execution of the program returns to the directory it started in; so we need to include multiple statements per line within our calls to shell.

Of course, you don't have to enter every command as a separate line of Pascal; you could create a script file like this (from fpc buildfaq):

#!/bin/sh
cd /usr/local/src/fpc/devel
cd fpc
make clean
cd ..
cd lazarus
make clean
cd ..
svn up fpc >& ~/cvscheckout.log
svn up lazarus >& ~/cvslaz.log 

Name it updatelaz.sh, and then call it from a Pascal program thus:

Program LazUpdate1;
uses classes, unix;
var S : longint; 
begin
  S := fpsystem ('updatelaz.sh')
end.

コマンドラインパラメータ

See Command line parameters and environment variables.

Orginal contributors

This page has been converted from the epikwiki version. Original content by User:Kirkpatc.