PDA

View Full Version : Programmer's guide to FPC and Lazarus



Almindor
15-01-2005, 01:09 PM
I'm writing this guide because I noticed people seem to experience the same
problems I had with FPC and Lazarus and same questions are asked all over.
This guide is for anyone inexperienced in getting FPC and Lazarus to work properly.

HISTORY: 15.7.2007 - updated with new smartlink image
15.9.2007 - updated with new FPC version info
------------------------------------------------------------------------------------
FPC: (read only if you DON'T want to use Lazarus, otherwise skip to next)

There are 2 ways to get FPC.
1st is to get the "all-in-one" package of precompiled fpc from
http://www.freepascal.org/download-ftp.freepascal.org.html
2nd is to use SVN as described on www.freepascal.org page.
NOTE: if you want to use precompiled Lazarus, don't get FPC as separate package.
You need a binary of FPC 2.0.4 if you wish to compile SVN fpc.
You can get them here.

After installing FPC ether way there is one more thing you need to check.
There's a file fpc.cfg which you must edit and make sure these lines point to the right paths:

Example:
-Fu/usr/local/lib/fpc/$fpcversion/units/$fpctarget
-Fu/usr/local/lib/fpc/$fpcversion/units/$fpctarget/*
-Fu/usr/local/lib/fpc/$fpcversion/units/$fpctarget/rtl
NOTE: $fpcversion is always eqal to the version number of the FPC binary which is currently in use. $fpctarget specifies the OS(don't edit them)

If you get an error like "unit SysUtils not found" it's because your FPC paths are not set correctly.
In windows you also need to specify PATH to your FPC executable so you can use it in command line.
There's a "setpath.bat" file in pp\bin\$fpctarget\ which will do this for you.
To test if FPC works correctly try to compile a simple hello world program which uses some FCL unit like for example "xmlcfg";(just add it to uses section)
If the path is right everything should compile OK.

-------------------------------------------------------------------------------------
Lazarus:

As with FPC there are 2 ways to get Lazarus.

1st: Get it from www.lazarus.freepascal.org as a binary package.
NOTE: for linux users you need:
fpc-xxx.rpm
fpcsrc-xxx.rpm
lazarus-xxx.rpm
You DON'T need the .src.rpm packages. The XXX must be same for all 3 packages. Install fpc and fpcsrc rpm's 1st.
Windows users must only get the setup executable and install it.

2nd: SVN. If you want to use Lazarus from SVN you need to have working FPC installed. See sections above. To get Lazarus from SVN you need a working SVN client. For unix users, just install "subversion" package. In windows I personaly use Tortoise SVN.

This command will download SVN Lazarus into the subdirectory "lazarus" from current path (eg: I'm in /home/ales I will get /home/ales/lazarus):


svn co http://svn.freepascal.org/svn/lazarus/trunk lazarus


This downloads the latest development version of Lazarus. If you need some stable version from SVN, search the svn/lazarus subdir
(example: svn ls http://svn.freepascal.org/svn/lazarus)

Now all that remains is:


cd lazarus
make


In case you have freeBSD you need to use "gmake" instead of make.

After you run lazarus for the first time it may ask you about FPC paths.
Apart from FPC source path and make path, all others are REQUIRED.
You need to specify where Lazarus itself is, where FPC ppc386[.exe] is
and if you have FPC sources it's good to specify those too(so you can browse them with CTRL+CLICK)
Path to "make" is not required. In FreeBSD you need to set path to make to point to the "gmake" binary (find out with "which gmake").

-------------------------------------------------------------------------------------
Getting GDB to work.

For windows:

If you installed from setup.exe you already have everything done for you.

Otherwise do this:

Get mingw package with GDB(snapshot version 6.3). Install it somewhere.
Go to enviroment options/debugger choose gdb(without SSH) and set the path to the gdb executable.

In windows it's probably C:\mingw\bin\gdb.exe
In linux it's probably /usr/bin/gdb

NOTE: You CAN'T run programs with GDB if you strip GDB info or use run-parameter application.

Also make sure you have Generate Debugging Info For GDB checked in if you want to use GDB.(-gl is not enough)
Smartlinking must be also OFF and I'd advise agains optimalizations too.

-------------------------------------------------------------------------------------
Reducing filesize with Lazarus:
All OSes: To reduce the filesize go to
Project/Compiler Options/Linking
Uncheck all except Strip Symbols From Executable (-Xs)
and Check Link Smart(-XX)

This should produce filesizes ~1Mb.
If you get bigger than 2Mb in windows start the command line and go to your project. If you have FPC in path just type "strip filename.exe"

Your link tab should look like this:
http://members.chello.sk/ales/data/laz_link.png

Config Options

To reduce filesize even more you can use UPX.
UPX is provided with FPC in windows. You will need to get it in Linux yourself.
If you have FPC in PATH "upx filename.exe"(same for linux if you got UPX)
You should get filesize about 500kb.

WARNING: UPX brakes program parameters(paramstr) in Linux because it first uncompresses it to /tmp. If you use paramstr(0) for something don't use UPX.

I hope this guide will provide useful and perhaps make some of you with negative experiences with FPC reconsider. If you find any errors/mistakes or suggestions send me a mail.

------------------------------------------------------------------------------------
Use of libraries and external code:

I've noticed that many people have problems compiling applications which
use some sort of "external" code. Most notably people download some
contributed units/package and can't set the paths right. He's an example
on how to set up JEDI-SDL.

Download and unpack jedi-sdl somewhere. Copy the directory SDL
to your projects directory (example: /home/user/MyProject/SDL)
Now to be able to compile your application which uses SDL you need
to add: -FuSDL and -FiSDL when compiling like so:
"fpc -FuSDL -FiSDL MyProgram.pas"
If you use Lazarus, you can do this when you fill in the
Project/Compiler Options/Other Units and Includes
You'll need to do this for every project which uses some external
units/ppus.
NOTE: a .ppu file is "pascal plus unit" it's a compiled FPC unit.
You can use ppu files without their sources but you won't be able
to use things like code-completion if you don't have their sources.[/code]