Sign in Join
Wiki copytemporary

Lazarus and Free Pascal on WSL2: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Can a Linux FPC-generated 'hello world' be run unmodified on WSL-2 ?
22
33Yes
44linux FPC-generated 'hello world' can run unmodified on WSL-2.
55
66More complex applications can run (i.e. FPC itself installed from a repository)
77
88## Can the Linux version of FPC be installed and run unmodified on WSL2 ?
99
1010In particular: the program and compiler issue direct kernel calls.
1111
1212We need to know if this is working, or if a LIBC linked version of the Linux binaries is needed.
1313
1414Yes
1515The easiest way is to install through the repository.
1616
1717Direct Kernel calls work fine under WSL2.
1818
1919## Can a Windows executable execute a WSL2 program, in particular, the compiler.
2020
2121If yes, this must be demonstrated this using a Win32 FPC program.
2222
2323If not, document what the problems are.
2424
2525Yes (potential problems)
2626There's [wslwrap](https://github.com/skalogryz/wslwrap) (Win32 FPC program) application provided that's executing WSL2 program.
2727
2828
2929There's an outstanding issue with truncating and output buffer, if pipe redirection is used:
3030
3131
3232<https://github.com/microsoft/WSL/issues/4082>
3333
3434<https://github.com/microsoft/WSL/issues/4184>
3535
3636This limits the application of WSL application, due to possible characters missing in the output.
3737
3838## Can the Lazarus IDE execute the WSL2 Linux compiler ?
3939
4040Yes
4141The only tested approach is a quick substitute of the compiler executable, that runs WSL2 linux compiler.
4242
4343One of the approaches is described at: <https://wiki.freepascal.org/Windows_Subsystem_for_Linux#Quick_and_Dirty_-_Using_WSL_FPC_from_Windows>
4444
4545Another approach using the latest wslwrap version is to replace fpc.exe (make a backup!) with wslwrap.exe (copying over) and creating fpc.wrp file with
4646
4747
4848exe=fpc
4949
5050This will allow to compile -TLinux projects, BUT it's also causing interference in IDE itself, as IDE components (i.e. Codetools) depends on the "windows" version of FPC used.
5151
5252Windows vs Linux paths are creating conflicts when using WSL compiler. Examples are of such conflicts are (The project was configured to use a wsl-wrapped FPC):
5353
54-`Warning: [TPCTargetConfigCache.Update] cannot find real compiler for this platform: Compiler="C:\FPC_Laz\wsltools\fpc.exe" Options="-FrC:\lazarus\components\codetools\fpc.errore.msg" RealCompiler="C:\Users\Owner\AppData\Local\lazarus\/usr/lib/fpc/3.0.0/ppcx64"`\
54+```text
55+Warning: [TPCTargetConfigCache.Update] cannot find real compiler for this platform: Compiler="C:\FPC_Laz\wsltools\fpc.exe" Options="-FrC:\lazarus\components\codetools\fpc.errore.msg" RealCompiler="C:\Users\Owner\AppData\Local\lazarus\/usr/lib/fpc/3.0.0/ppcx64"
5556
56-`Hint: [TPCTargetConfigCache.NeedsUpdate] TargetOS="linux" TargetCPU="x86_64" Options="" real compiler file changed "C:\Users\Owner\AppData\Local\lazarus\/usr/lib/fpc/3.0.0/ppcx64"`\
57+Hint: [TPCTargetConfigCache.NeedsUpdate] TargetOS="linux" TargetCPU="x86_64" Options="" real compiler file changed "C:\Users\Owner\AppData\Local\lazarus\/usr/lib/fpc/3.0.0/ppcx64"
5758
58-`Warning: [TPCTargetConfigCache.Update] fpc.exe found cfg a file, the IDE did not: "C:\Users\Owner\AppData\Local\lazarus\\etc\fpc.cfg"`
59+Warning: [TPCTargetConfigCache.Update] fpc.exe found cfg a file, the IDE did not: "C:\Users\Owner\AppData\Local\lazarus\\etc\fpc.cfg"
60+```
5961
6062
6163**Note**: a custom compiler can be specified per project (+per build mode?), rather than the entire environment .
6264
6365## Can a Windows executable execute & debug a WSL2 program using WSL2 gdb ?
6466
6567(This is assuming that a WSL2 version of gdb exists. If not, document this.).
6668
6769In particular, can Lazarus use the WSL2 gdb if such exists.
6870
6971Practically - No (Potentially- Yes)
7072Gdb does present in WSL2 and needs to be installed additionally
7173
7274While it's possible to run WSL2 gdb (and it's surely possible to configure Lazarus to install Gdb)
7375
7476Due to glitches with WSL2 pipe redirection, it's not possible to get gdb reports on time (whenever gdb stops on break point or an error). Lazarus IDE is unable detect the change in the execution.
7577
7678### Setting up a project
7779
7880
79811\. Copy wslwrap.exe twice. One for fpc.exe and another for gdb.exe.
8082
81832\. Create corresponding .wrp file
8284
8385
8486gdb.wrp should look like this:
8587
86-`exe=gdb`\
87-`inp.dummy=0`\
88-`inp.wintonix=1`\
89-`shell=1`
88+```text
89+exe=gdb
90+inp.dummy=0
91+inp.wintonix=1
92+shell=1
93+```
9094
9195
9296
9397the debugger doesn't need to have "dummy" input, because a normal commands would be sent by IDE
9498
9599the all paths in the commands needs to be replaced. Lazarus is Win, and it sends Windows Paths, those has to be converted to linux paths (so gdb would seet hem)
96100
97101shell must be used, as Ggb checks for the environment. If it doesn't detect a proper shell it would use "exec" command in order to launch a child process, which would eventually fail.
98102
99103![](/wiki-images/1/1e/Wsl-setting-up.png)
100104
101105
1021063\. Specify the new compiler executable in the project setting (not to mess up with IDE)
103107
104108![](/wiki-images/f/fc/Wsl-compiler-proj.png)
105109
106110
1071113.1 You also want to uncheck "Apply conventions" for "Target name" of "Compiler Options/Paths" and update "Target file name" by removing ".exe" extension.
108112
109113![](/wiki-images/4/40/Wsl-project-compiler-options.png)
110114
111115The IDE will start sending a lot of warning regarding non existing configuration files and unsupported targets. Those can be ignored as files would be used by the compiler from WSL and those configuration and files are present in WSL environment.
112116
113117
1141184\. Specify the wrapped gdb file name in debugger options
115119
116120![](/wiki-images/8/89/Wsl-debugger-options.png)
117121
118122
1191235\. Run the project.
120124
121125![](/wiki-images/6/6d/Wsl_debug.png)
122126
123127## Can the FPC windows 64-bit compiler cross-compile a -Tlinux program using WSL 2. If yes, how ?
124128
125129Yes (but undesired approach?)
126130It's possible to use WSL2 utils as cross-compiler binary utils. For example an executable x86_64-linux-ld.exe can be substituted with call to "wsl ld", the link.res file generated by FPC is still using windows paths. Preventing the linux linker from being able to build a binary.
127131
128132It's possible to update link.res prior to the execution of ld, and convert Windows paths to WSL (Linux) paths.
129133
130134Steps (assumption is made that Win32 FPC doesn't have any cross-compile utils installed):
131135
132136
1331371\. have WSL2 installed
134138
1351392\. install FPC to WSL2 matching version that is matching the one currently used on Windows. (this is critical as FPC would match units used). (for example the official Lazarus release comes with 3.0.4. Distro's repository would have FPC 3.0.0 available. This won't work for cross-compiling, FPC 3.0.4 needs to be isntalled)
136140
1371412.1 download (in win32) linux-xxx.xxx.tar from the official FPC
138142
1391432.2 unpack tar file
140144
1411452.3 run "wsl sudo ./install.sh" (sudo is need to be able to install to default /usr dir)
142146
1431473\. copy x86_64-linux units from WSL to your windows FPC directory (i.e. C:\lazarus\fpc\3.0.4\units\x86_64-linux). You can do that by running the following in Windows command line
144148
145149
146150
147151cd "C:\lazarus\fpc\3.0.4\units"
148152
149153wsl cp -r /usr/lib/fpc/3.0.4/units/x86_64-linux ./
150154
1511554\. Get the latest [wslwrap](https://github.com/skalogryz/wslwrap) project and compile it.
152156
1531574.1 Move wslwrap.exe to C:\lazarus\fpc\3.0.4\bin\x86_64-linux\x86_64-linux-ld.exe (not the destinatino folder might not exit)
154158
1551594.2 create the file C:\lazarus\fpc\3.0.4\bin\x86_64-linux\x86_64-linux-ld.wrp. Add two following lines
156160
157161
158162exe=ld
159163
160164linkres=1
161165
162166This instructs the current executable when doing call over WSL to cal "ld", but also update link.res file prior to that
163167
1641685\. Done. Try to compile hello world pas using command line:
165169
166170
167171C:\lazarus\fpc\3.0.4\bin\x86_64-win64\fpc.exe -Tlinux test.pas
168172
169173as a result you should get "test" binary, that can be ran "wsl ./test"
170174
171175The problem is that might require to build more "exceptions" and "wsl" hacks in future. Using straight forward Linux cross-compiler might be more reliable approach - OR - instead using WSL calling directly, rather than as a cross-compiler.
172176
173177Also attempt to use "Math" unit fails immediately due to IE 200208151: <https://bugs.freepascal.org/view.php?id=35781>
174178
175179## Summary
176180
177181- WSL2 still requires its own fixes.
178182- a number of changes are required in Lazarus IDE to make it "wsl" aware. (so it would use WSL paths, instead of Windows paths). Without that changes, even a smarter "wslwrap" utility would have its own challenges.
179183- no changes are needed in FPC. Though fixing the internal error would make a better use of wsl as cross-compiler.
180184
181185## See Also
182186
183187- [[WSL]]
184188
185189---
186190*From the Free Pascal wiki, page "Lazarus and Free Pascal on WSL2", as it stood on 2019-07-12 after a change by Skalogryz, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
187191Category: Linux
188192Category: Windows

Back to history