Recent

Author Topic: [SOLVED] Linux/Ubuntu: how to compile 32 bit programs on a 64 bit Lazarus?  (Read 5622 times)

Hartmut

  • Sr. Member
  • ****
  • Posts: 281
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
« Last Edit: August 28, 2018, 07:53:29 pm by Hartmut »

Handoko

  • Hero Member
  • *****
  • Posts: 3237
  • My goal: build my own game engine using Lazarus
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #1 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.

Hartmut

  • Sr. Member
  • ****
  • Posts: 281
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #2 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?

Handoko

  • Hero Member
  • *****
  • Posts: 3237
  • My goal: build my own game engine using Lazarus
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #3 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.

Hartmut

  • Sr. Member
  • ****
  • Posts: 281
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #4 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?

Handoko

  • Hero Member
  • *****
  • Posts: 3237
  • My goal: build my own game engine using Lazarus
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #5 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

lucamar

  • Hero Member
  • *****
  • Posts: 2144
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #6 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?
« Last Edit: August 27, 2018, 08:14:44 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Hartmut

  • Sr. Member
  • ****
  • Posts: 281
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #7 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).
« Last Edit: August 27, 2018, 08:30:20 pm by Hartmut »

Handoko

  • Hero Member
  • *****
  • Posts: 3237
  • My goal: build my own game engine using Lazarus
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #8 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?

Hartmut

  • Sr. Member
  • ****
  • Posts: 281
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #9 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?

Handoko

  • Hero Member
  • *****
  • Posts: 3237
  • My goal: build my own game engine using Lazarus
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #10 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.

Hartmut

  • Sr. Member
  • ****
  • Posts: 281
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #11 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!

lucamar

  • Hero Member
  • *****
  • Posts: 2144
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #12 on: August 27, 2018, 09:19:47 pm »
If I were you I would:
  • open Synaptic
  • search for fpc-src
  • if installed, purge it (right-click, select "uninstall completely" and apply)
  • mark it again for install and apply
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
« Last Edit: August 27, 2018, 09:49:49 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Handoko

  • Hero Member
  • *****
  • Posts: 3237
  • My goal: build my own game engine using Lazarus
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #13 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
« Last Edit: August 27, 2018, 09:29:59 pm by Handoko »

Hartmut

  • Sr. Member
  • ****
  • Posts: 281
Re: Linux/Ubuntu: how to compile 32 bit programs?
« Reply #14 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
« Last Edit: August 27, 2018, 09:54:19 pm by Hartmut »