# [fpc-pascal] Cross-compilation to Linux from win32

Skybuck@home.nl skybuck at home.nl
Sat Jan 15 15:41:18 CET 2005

----- Original Message -----
From: "Simon Kissel" <scamp at untergrund.net>
To: <fpc-pascal at lists.freepascal.org>
Sent: Friday, January 14, 2005 7:55 PM
Subject: [fpc-pascal] Cross-compilation to Linux from win32

> Hi,
>
> after quite some time I thought to give FPC another try to see if it's
> a suitable replacement for Kylix.
>
> My goal is to be able to cross-compile Linux elf targets from win32.
>
> packages for Linux.
>
> Run up FP, compiled hello world, worked. Switched the target in FP to
> linux, compiled again - and wow, it also worked. Using the verbose
> output I could even see it using the Linux units etc. All fine.
>
> Until I examined the generated executable "hello". It looks like it's
> a Linux binary packed into a PE file. Very weird. Doesn't run under
> Linux of course, and does nothing under Windows.
>
> Next attempt was to use fpc directly:
>
> C:\pp\demo\text>fpc -Aelf hello.pp
> Free Pascal Compiler version 1.9.6 [2004/12/31] for i386
> Copyright (c) 1993-2004 by Florian Klaempfl
> Target OS: Win32 for i386
> Compiling hello.pp
> 31 Lines compiled, 0.4 sec
>
> Well, no.
>
> Some advise on what to do would be very welcome.
>
> Thanks,
>
> Simon
>
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Hi,

A cross compiler works on a specific operating system (the host) and
compiles/links code for another processor/operating system (the target)

I think one needs the following things to build a free pascal
cross-compiler:

1. A cross compiler which can be executed/run on the host computer and
compiles to the target computer. (as.exe will be the cross compiler )

2. A cross linker which can be executed/run on the host computer and links
to the target computer.
( ls.exe will be the cross linker )

3. Free Pascal Compiled Units for the target computer.
( c:/pp/units/linux )

( I don't know how to compile these units for the target computer, I

I build a free pascal cross compiler once.

It compiles from win32 to linux.

Here some tips:

1. Install Free Pascal ( Binary )

3. Install BinUtils-2.14 (or maybe another version) ( Source )

4. Find somebody with compiled free pascal linux units. Place them in
c:/pp/units/linux

5. Go in CygWin

6. Type: which make

7. Check if it's cygwin's make. If it's the wrong version than alter
cygwin.bat and change the path so that only windows folders and this path is
in it: c:\cygwin\bin

8. Go to the binutils folder

To list folders and files type: ls

To go to root type: cd c:/

To go to binutils type: cd /binutils-2.14

Make a new build folder by typing: mkdir build

Type: cd build

Type: ../configure --target=i686-linux

This means it executes the program in the parent folder and places the
output in the current folder which should be c:/binutils-2.14/build

This configure program will make a new make file so that the binutils can be
compiled for linux

BinUtils contains a as.exe  ( cross assembler ) and a ld.exe ( cross
linker ) you need these two for the cross compiler.

9. Disable firewalls and virus scanners, plug the network cables.

10. Type: make

It will take very long to make the new as-new.exe and ld-new.exe

11. After it's done you have two new files:

as-new.exe is located in build\as\

ld-new.exe is located in build\ld\

12.

Execute as-new.exe with the parameter -v

This parameter will show you if the target is indeed linux.

If you get errors will doing this you are using the wrong cygwin1.dll. There
could be multiple copies of

The solution is to copy the cygwin1.dll file from c:\cygwin\bin to the
as-new folder.

13. Copy as-new.exe to c:\pp\bin\linux and rename it to as.exe
14. Copy ld-new.exe to c:\pp\bin\linux and rename it to ld.exe

15. Change the windows path variable so that c:\cygwin\bin is removed from
it.

If you don't do this then free pascal cross compiler might use the wrong
as.exe and ld.exe.

16. Make a simply hello world example and compile it like this:

fpc -TLINUX hello.pas

The result will be a linux executable without an extension.

Go to linux and type:

./hello

To run it.

If you uploaded it to a linux machine via ftp or something and you are using
ssh/putty whatever you might first need to give it execute permissions etc.

Good luck to you.
Bye,
Skybuck.