Sign in Join
Wiki copytemporary

Lazarus and Free Pascal on WSL2: what changed

Revision 1 to 2. 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
5454`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"`\
55-\
55+
5656`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-\
57+
5858`Warning: [TPCTargetConfigCache.Update] fpc.exe found cfg a file, the IDE did not: "C:\Users\Owner\AppData\Local\lazarus\\etc\fpc.cfg"`
5959
6060
6161**Note**: a custom compiler can be specified per project (+per build mode?), rather than the entire environment .
6262
6363## Can a Windows executable execute & debug a WSL2 program using WSL2 gdb ?
6464
6565(This is assuming that a WSL2 version of gdb exists. If not, document this.).
6666
6767In particular, can Lazarus use the WSL2 gdb if such exists.
6868
6969Practically - No (Potentially- Yes)
7070Gdb does present in WSL2 and needs to be installed additionally
7171
7272While it's possible to run WSL2 gdb (and it's surely possible to configure Lazarus to install Gdb)
7373
7474Due 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.
7575
7676### Setting up a project
7777
7878
79791\. Copy wslwrap.exe twice. One for fpc.exe and another for gdb.exe.
8080
81812\. Create corresponding .wrp file
8282
8383
8484gdb.wrp should look like this:
8585
8686`exe=gdb`\
8787`inp.dummy=0`\
8888`inp.wintonix=1`\
8989`shell=1`
9090
9191
9292
9393the debugger doesn't need to have "dummy" input, because a normal commands would be sent by IDE
9494
9595the 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)
9696
9797shell 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.
9898
99-<figure>
100-<img src="wsl-setting-up.png" title="wsl-setting-up.png" />
101-<figcaption>wsl-setting-up.png</figcaption>
102-</figure>
99+![](/wiki-images/1/1e/Wsl-setting-up.png)
103100
104101
1051023\. Specify the new compiler executable in the project setting (not to mess up with IDE)
106103
107-<figure>
108-<img src="wsl-compiler-proj.png" title="wsl-compiler-proj.png" />
109-<figcaption>wsl-compiler-proj.png</figcaption>
110-</figure>
104+![](/wiki-images/f/fc/Wsl-compiler-proj.png)
111105
112106
1131073.1 You also want to uncheck "Apply conventions" for "Target name" of "Compiler Options/Paths" and update "Target file name" by removing ".exe" extension.
114108
115-<figure>
116-<img src="wsl-project-compiler-options.png" title="wsl-project-compiler-options.png" />
117-<figcaption>wsl-project-compiler-options.png</figcaption>
118-</figure>
109+![](/wiki-images/4/40/Wsl-project-compiler-options.png)
119110
120111The 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.
121112
122113
1231144\. Specify the wrapped gdb file name in debugger options
124115
125-<figure>
126-<img src="wsl-debugger-options.png" title="wsl-debugger-options.png" />
127-<figcaption>wsl-debugger-options.png</figcaption>
128-</figure>
116+![](/wiki-images/8/89/Wsl-debugger-options.png)
129117
130118
1311195\. Run the project.
132120
133-<figure>
134-<img src="wsl_debug.png" title="wsl_debug.png" />
135-<figcaption>wsl_debug.png</figcaption>
136-</figure>
121+![](/wiki-images/6/6d/Wsl_debug.png)
137122
138123## Can the FPC windows 64-bit compiler cross-compile a -Tlinux program using WSL 2. If yes, how ?
139124
140125Yes (but undesired approach?)
141126It'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.
142127
143128It's possible to update link.res prior to the execution of ld, and convert Windows paths to WSL (Linux) paths.
144129
145130Steps (assumption is made that Win32 FPC doesn't have any cross-compile utils installed):
146131
147132
1481331\. have WSL2 installed
149134
1501352\. 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)
151136
1521372.1 download (in win32) linux-xxx.xxx.tar from the official FPC
153138
1541392.2 unpack tar file
155140
1561412.3 run "wsl sudo ./install.sh" (sudo is need to be able to install to default /usr dir)
157142
1581433\. 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
159144
160145
161146
162147cd "C:\lazarus\fpc\3.0.4\units"
163148
164149wsl cp -r /usr/lib/fpc/3.0.4/units/x86_64-linux ./
165150
1661514\. Get the latest [wslwrap](https://github.com/skalogryz/wslwrap) project and compile it.
167152
1681534.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)
169154
1701554.2 create the file C:\lazarus\fpc\3.0.4\bin\x86_64-linux\x86_64-linux-ld.wrp. Add two following lines
171156
172157
173158exe=ld
174159
175160linkres=1
176161
177162This instructs the current executable when doing call over WSL to cal "ld", but also update link.res file prior to that
178163
1791645\. Done. Try to compile hello world pas using command line:
180165
181166
182167C:\lazarus\fpc\3.0.4\bin\x86_64-win64\fpc.exe -Tlinux test.pas
183168
184169as a result you should get "test" binary, that can be ran "wsl ./test"
185170
186171The 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.
187172
188173Also attempt to use "Math" unit fails immediately due to IE 200208151: <https://bugs.freepascal.org/view.php?id=35781>
189174
190175## Summary
191176
192177- WSL2 still requires its own fixes.
193178- 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.
194179- no changes are needed in FPC. Though fixing the internal error would make a better use of wsl as cross-compiler.
195180
196181## See Also
197182
198183- [[WSL]]
199184
200185---
201186*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).*
202187Category: Linux
203188Category: Windows

Back to history