Lazarus

Programming => Operating Systems => Linux => Topic started by: Hartmut on August 27, 2018, 05:42:57 pm

Title: [SOLVED] Linux/Ubuntu: how to compile 32 bit programs on a 64 bit Lazarus?
Post by: Hartmut on August 27, 2018, 05:42:57 pm
I am trying to adapt my very first free pascal program from Windows to Linux with Lazarus.

I used Windows 7 (32 bit) and all my programs were developed as 32 bit programs (where pointers have 4 bytes etc.).
My Linux (Ubuntu 18.04) is a 64 bit system. I installed Lazarus 1.8.2 via "sudo apt-get install lazarus". Now pointers have a size of 8 bytes.

To make the beginning a little easier, I would like to compile my programs on Linux first as 32 bit programs (assuming that it is possible to run a 32 bit program on a 64 bit Linux, as it is possible on Windows). Is this possible?

If yes, how can I switch my Lazarus from 64 bit to 32 bit? Is there a setting for that or must I install Lazarus new?
I tried Menu Project / Project Options / Compiler Options / Config and Target / Target CPU family = i386 but then got error message  "Compiler "/usr/bin/fpc" does not support target i386-linux".

I am a bloody beginner on Linux (started just a few days ago). So please be a little more detailed with your answers. Thanks a lot in advance.

There is a conclusion about the solution (how to cross compile 32 bit programs with a 64 bit Lazarus) in reply #29
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Handoko on August 27, 2018, 06:25:13 pm
As far as I know, Ubuntu and maybe other Linux also are going to drop support for i386.

But if you want to learn how to cross compile to Linux 32-bit from Linux 64-bit, these are things you need to do:

01. You need to instal libc6-dev-i386 package
02. Use a text editor type as --32 $@ and save it as /usr/bin/i386-linux-as
03. Use a text editor type ld -A elf32-i386 $@ and save it as /usr/bin/i386-linux-ld
04. sudo chmod +x /usr/bin/i386-linux-as
05. sudo chmod +x /usr/bin/i386-linux-ld
06. cd /usr/share/fpcsrc/3.0.4
07. sudo make clean all CPU_TARGET=i386
08. sudo make crossinstall OS_TARGET=linux CPU_TARGET=i386  INSTALL_PREFIX=/usr
09. sudo ln -sf /usr/lib/fpc/3.0.4/ppcross386 /usr/bin/ppc386

Note:
You may need to change "3.0.4" (steps #6 and #9) to the version of your FPC.

It is a bit hard to understand the steps above, you need to have some knowledge about Linux Terminal.

You can search the web, all the information to cross compile from Lazarus 64-bit to 32-bit are there.
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 27, 2018, 07:18:13 pm
Hello Handoko, thank you very much for your answer and that detailed instructions. I could execute steps 1..6 without problems.
But in step 7 I got an error message "make: *** Keine Regel, um „clean“ zu erstellen.  Schluss". This means "no rule to create "clean". Finish" (see attached screenshot)
What shall I do?
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Handoko on August 27, 2018, 07:26:12 pm
Oh, sorry I forgot:
Both step #2 and #3, you need to put this in the first line #!/bin/bash . So each of the files contains 2 lines.
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 27, 2018, 07:52:22 pm
I inserted "#!/bin/bash" in both files and repeated steps 4, 5 and 7, but got the same error-message as before. In the screenshot you see both files and in the upper left the files with their access rights. What do I wrong?
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Handoko on August 27, 2018, 08:06:57 pm
If you can't make clean (step #7), try without clean:
sudo make all CPU_TARGET=i386
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: lucamar on August 27, 2018, 08:10:47 pm
[...] in step 7 I got an error message "make: *** Keine Regel, um „clean“ zu erstellen.  Schluss". This means "no rule to create "clean". Finish" (see attached screenshot)

Open a terminal in /usr/share/fpcsrc/3.0.4 and type:
  grep "^clean:" Makefile
It should give something like this:
Code: [Select]
lucamar@luna:/usr/share/fpcsrc/3.0.4$ grep "^clean:" Makefile
clean: $(addsuffix _distclean,$(TARGET_DIRS)


If not, your Makefile may have become corrupted. It can be rebuilt but I don't know exactly how: using fpcmake? some other way?
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 27, 2018, 08:27:42 pm
I tried "sudo make all CPU_TARGET=i386" and got the error message "no rule to create "all". Finish" (see attached screenshot).

Then I tried "grep "^clean:" Makefile" and got the answer "grep: Makefile: file or folder not found" (see same screenshot).

I looked into folder "/usr/share/fpcsrc/3.0.4/" and it contains no files, but 3 folders:
   compiler
   packages
   rtl
and each of this 3 folders contains a file "Makefile.fpc" (but they are all different).
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Handoko on August 27, 2018, 08:34:27 pm
I looked at my 3.0.4 folder, it contains lots of files. Maybe your installation has problem? Have you install all the 3 deb files properly?
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 27, 2018, 08:46:49 pm
I only installed "sudo apt-get install lazarus", thats all. With that installation I could start Lazarus and compile a very simple program and execute it successfully. Must I install more?
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Handoko on August 27, 2018, 08:48:56 pm
Do you have Makefile in the 3.0.4 folder? If no, you can use mine. This Makefile contains the rules. Unzip the file and save it to your 3.0.4 folder.
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 27, 2018, 09:11:02 pm
I unzipped "Makefile" into "/usr/share/fpcsrc/3.0.4/" and repeated step 7. I got a longer error message (see screenshot) which means:
   Folder "/usr/share/fpcsrc/3.0.4/compiler" is entered
   No rule to create "distclean“. Finish.
   Folder "/usr/share/fpcsrc/3.0.4/compiler" is left
   Makefile:2509: recipe for target 'compiler_distclean' failed
   make: *** [compiler_distclean] Error 2
File "Makefile" has rights "rw-"
Thanks a lot for helping me!
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: lucamar on August 27, 2018, 09:19:47 pm
If I were you I would:
If those files Handoko shows are not in there then something is wrong with Ubuntu's install.

ETA Hmmm ... I forgot  :-[
When uninstalling fpc-src Synaptic may want you to uninstall lazarus-project too. If so, let it and reinstall it after re-installing fpc-src
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Handoko on August 27, 2018, 09:27:33 pm
   Folder "/usr/share/fpcsrc/3.0.4/compiler" is entered
   No rule to create "distclean“. Finish.

Does your .../3.0.4/compiler folder contain the things like mine? It should have its own Makefile too.

I think you have problem with your installation. Try to reinstall your Lazarus/FPC before performing the cross compilling. Try what @lucamar said first, if it doesn't work maybe this can be useful:
http://forum.lazarus.freepascal.org/index.php/topic,41524.0.html
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 27, 2018, 09:50:40 pm
My folder "/usr/share/fpcsrc/3.0.4/compiler" looks very similar to Handoko's (see screenshot), but in mine there is only a file "Makefile.fpc", but no file "Makefile".

I searched in Synaptic for "fpc-src" as name and description but absolute nothing was found... See screenshot.
But I found "fpc-source" (which is not installed) and "fpc-source-3.0.4" (which is installed).
@lucamar: Thank you too for helping me.

Meanwhile I will read http://forum.lazarus.freepascal.org/index.php/topic,41524.0.html
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: lucamar on August 27, 2018, 10:26:25 pm
[...] I found "fpc-source" (which is not installed) and "fpc-source-3.0.4" (which is installed).

Yeah, sorry, fpc-source-3.0.4 is the one. Mine is called fpc-src because I install from a local repository. As for fpc-source, it's simply a dependency package to pull in the latest version; you can ignore it since lazarus-project picks its own proper dependencies anyway.

The idea, as you may have gleaned, is to induce a full re-install of your current source package to see whether the culprit is the package itself or some other thing.

@lucamar: Thank you too for helping me.

Well, that's what the fora are for, usually. :)
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 27, 2018, 11:08:26 pm
Via Synaptic I "uninstalled completely" fpc-source-3.0.4
Via Terminal I deinstalled Lazarus 1.8.2 via "sudo apt-get purge lazarus"
Via Terminal I installed Lazarus again via "sudo apt-get install lazarus"
Package "libc6-dev-i386" from step 1 was already installed.
The 2 files from steps 2..5 already existed.
The one and only file in folder "/usr/share/fpcsrc/3.0.4/" was the "Makefile" from Handoko's reply #10.
I repeated steps 6 and 7. The translated error message (see screenshot) is now:

/bin/rm -f build-stamp.*
/bin/rm -f base.build-stamp.*
make compiler_cycle RELEASE=1
make[1]: Folder „/usr/share/fpcsrc/3.0.4“ is entered
make -C compiler cycle
make[2]: Folder „/usr/share/fpcsrc/3.0.4“ is entered
make[2]: *** compiler: File or folder not found. Finish.
make[2]: Folder „/usr/share/fpcsrc/3.0.4“ is left
Makefile:2780: recipe for target 'compiler_cycle' failed
make[1]: *** [compiler_cycle] Error 2
make[1]: Folder „/usr/share/fpcsrc/3.0.4“ is left
Makefile:2812: recipe for target 'build-stamp.i386-linux' failed
make: *** [build-stamp.i386-linux] Error 2

What can I do?
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: lucamar on August 27, 2018, 11:26:30 pm
I declare myself routed: I don't know what else you can do, short of taking "desperate" measures---i.e. installing everything by hand after a thorough clean-up and double-checking each step.

I'm sorry.  :'(
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: lucamar on August 27, 2018, 11:31:30 pm
ETA

Via Synaptic I "uninstalled [...]
Via Terminal I deinstalled [...]
Via Terminal I installed [...]

You do realize that you could have done all that with either Synaptic or via terminal, don't you? No need to change for each step: just use the tool you want.

ETAA: Sorry about the double-posting; don't know how it happened---I *did* click "modify", not "reply"!
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Handoko on August 28, 2018, 05:07:24 am
@Hartmut

Please make sure your Lazarus, FPC and FPC-src are installed correctly. If you have fpc-source installed, it means you're using the installation package provided from Ubuntu. If you manually install Lazarus (using the deb files from SourceForge), it should be fpc-src.

I managed to combine Lazarus from SourceForge with FPC from Ubuntu repository but it didn't work.

You may need to do a clean installation, so use these below to do the full uninstallation first:
01. Uninstall lazarus, fpc, fpc-src
02. Delete ~/.lazarus, except: editoroptions, environmentoptions
03. Delete /etc/fppkg (folder)
04. Delete /etc/fp.*
05. Delete /usr/share/fpcsrc (folder)
06. Delete /usr/lib/fpc (folder)
07. Delete /usr/bin/ppc*
Note:
I keep those 2 files in step #2 because I want to keep some setttings I configured for Lazarus. You can delete them if you want.

After performing the complete uninstallation you now can instal the Lazarus/FPC. You may need a computer reboot. I know it sounds silly :o, but I have bad experience messing the installation, which causes I failed to install Lazarus/FPC no matter how hard I tried. I just shut down my pc, took a break wacthing tv, then came back to install, it worked effortlessly.

For installing Lazarus/FPC, you can follow the link I gave on the previous post.

After the Lazarus installed, you can test it by doing an IDE rebuild:
Lazarus main menu > Tools > Built Lazarus with Profile: Normal IDE

If you don't get any error when building the IDE, you now can continue to do the cross compiling using the steps I gave previously.
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 28, 2018, 12:59:15 pm
I deinstalled Lazarus as described in Reply #19.
I made a reboot.
I tried to install Lazarus as described in http://forum.lazarus.freepascal.org/index.php/topic,41524.0.html
I stucked in step 6: gdebi said that there are 63 additional packages neccessary, but could not download them because of Internet connection disabled (screenshot 11 says "Additional packages are downloaded" but there is no progress at all).
After 10-15 minutes I got a timeout error (screenshot 12 says "Could not download all neccessary files").

What shall I do? Install step 6 with Internet connection enabled?

Thanks a lot to Handoko for your continuous help.
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: SeregaKR on August 28, 2018, 01:13:51 pm
Lazarus installation on Linux is broken (I think so). Instead of installing 3 packages you have to install more than 10 from repository.
To fix it you have to purge all fpc* and lazarus* packages, download 3 packages from the web site (http://www.lazarus-ide.org/) and after moving this packages to home folder execute sudo dpkg -i ~/*.deb
DPKG will install all necessary requirements
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: SeregaKR on August 28, 2018, 01:15:40 pm
At least I used this method to deploy Lazarus on Linux Mint on the university computers.
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Handoko on August 28, 2018, 01:48:59 pm
@Hartmut

Tried @SeregaKR suggestion first, it is much simpler. If it doesn't work then use the installation trick I suggested.

I stucked in step 6: gdebi said that there are 63 additional packages neccessary, but could not download them because of Internet connection disabled ...

Then you may need to do it twice. First with the internet connection enabled, so your Ubuntu can install that 63 additional packages. Follow that steps until finished. But if your Lazarus can't start (because your it uses the its own Lazarus downloaded from Ubuntu repository), you need to repeat the installation again but with the internet connection disabled.
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 28, 2018, 03:11:53 pm
I deinstalled "fpc" und "fpc-src" via Synaptic "uninstall completely"
After that Synaptic said, nothing with lazarus* or fpc* was installed
To be sure I added "sudo apt autoremove"
Then I copied fpc_3.0.4-3_amd64.deb, fpc-src_3.0.4-2_amd64.deb and lazarus-project_1.8.4-0_amd64.deb to my home folder and executed "sudo dpkg -i ~/*.deb"

I got the following error message (see screenshot). Translated it means:

fpc-src (3.0.4) is configured ...
dpkg: dependence problemes prevent configuration of lazarus-project:
 lazarus-project depends of libgtk2.0-dev (>= 2.6.0); but:
  package libgtk2.0-dev is not installed.
dpkg: Error when processing package lazarus-project (--install):
 dependence problemes- stays unconfigured
Trigger for man-db (2.8.3-2) are processed ...
Trigger for gnome-menus (3.13.3-11ubuntu1.1) are processed ...
Trigger for desktop-file-utils (0.23-1ubuntu3.18.04.1) are processed ...
Trigger for mime-support (3.60ubuntu1) are processed ...
Trigger for shared-mime-info (1.9-2) are processed ...
Errors occured when processing of:
 lazarus-project

I found "libgtk2.0-dev" in Synaptic that it is installable. Shall I install it?
Must I then repeat "sudo dpkg -i ~/*.deb" ?

Thanks for your help!
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Handoko on August 28, 2018, 03:30:29 pm
I found "libgtk2.0-dev" in Synaptic that it is installable. Shall I install it?
Must I then repeat "sudo dpkg -i ~/*.deb" ?

Yes, libgtk2.0-dev is required for FPC on Linux Gtk.

@garlar27 collected a list of required libraries for Lazarus/FPC to run properly:
http://forum.lazarus.freepascal.org/index.php/topic,41524.msg288242.html#msg288242

After you installed libgtk2.0-dev, you need to repeat the sudo dpkg -i ~/*.deb.
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 28, 2018, 06:41:30 pm
Looks like we've made it!!

Yes, libgtk2.0-dev is required for FPC on Linux Gtk. After you installed libgtk2.0-dev, you need to repeat the sudo dpkg -i ~/*.deb.

I did both and got no error message.
After that I did an IDE rebuild as Handoko requested in reply #19 and got no errors.
Then I could repeat the 9 steps from Handoko's reply #1 without errors.

Now I can switch compiling between 32 bit and 64 bit via Lazarus Menu "Project / Project Options / Compiler Options / Config and Target / Target CPU family": "i386" => 32 bit / "Default" = 64 bit. I am happy!

The 1st program start after switching the bitness gives a debugger error (see screenshot), but you can ignore it and from the 2nd start on it does not come any more.

One question is left (which my sound stupid, but I'm a bloody beginner to Linux): how to start Lazarus normally? From the very 1st installation I had a desktop icon for that, but that was deinstalled.
After a long search I found:
  /usr/bin/lazarus-ide
  /usr/share/lazarus/1.8.4/lazarus
  /usr/share/lazarus/1.8.4/startlazarus
All 3 seem to do the same. But which of them is the "normal" way to start Lazarus? Then I will try to create a desktop Icon for that one...

Again thank you so much to everybody for your valuable help!
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Handoko on August 28, 2018, 06:50:33 pm
Put this command on your desktop icon or menu item:
startlazarus %f
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: lucamar on August 28, 2018, 07:31:56 pm
/usr/bin/lazarus-ide, like most (all?) lazarus-related commands there, is just a symlink; to, in this case, /usr/share/lazarus/1.8.4/lazarus.

startlazarus is indeed the "correct" incantation :)

And congratulations! In retrospective it should have been obvious that something was very wrong on your installation, so a complete reinstall was the best solution.
Title: Re: Linux/Ubuntu: how to compile 32 bit programs?
Post by: Hartmut on August 28, 2018, 07:50:43 pm
Because this topic got very long, I will give for future readers a short conclusion about the solution (how to cross compile 32 bit programs with a 64 bit Lazarus):
1) make sure that your lazarus installation is correct. If in doubt:
  a) uninstall all fpc* and lazarus* packages
  b) download 3 packages (fpc, fpc-src and lazarus) from the web site (http://www.lazarus-ide.org/)
  c) move this 3 packages to your home folder
  d) install package "libgtk2.0-dev"
  e) execute sudo dpkg -i ~/*.deb
2) do the 9 steps from reply #1 but take care of the correction for steps 2 and 3 in reply #3

Now you can switch compiling between 32 bit and 64 bit via Lazarus Menu "Project / Project Options / Compiler Options / Config and Target / Target CPU family": "i386" => 32 bit / "Default" = 64 bit.

The 1st program start after switching the bitness gives a debugger error (see screenshot in reply # 26), but you can ignore it and from the 2nd start on it does not come any more.

Good luck! And thanks a lot to all who helped me!

Update: 2 days later my Lazarus installation was automatically deinstalled by an automatic System-Update. So you must either use another installation way or protect your installation. I opened a new topic for that: http://forum.lazarus.freepascal.org/index.php/topic,42393.0.html