Sign in Join
Wiki copytemporary

Windows Subsystem for Linux: what changed

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

OldNewLine
11Microsoft introduced a certain compatibility functionality in order to run ELF binaries under Windows 10.
22
33## WSL1
44
55WSL1 is implemented as Microsoft developed Linux-kernel-compatible execution environment. This is not an actual Linux kernel, but only its substitute, which provides the most common functionality.
66
77The functionality is ENOUGH to run FPC without modifications.
88
99### Installing WSL and FPC
1010
1111Microsoft provides a number of Linux distros through Microsoft Store application. The following steps are using Ubuntu 16.4 LTS distro:
1212
1313- Install WSL
1414 - you can either install WSL through Powershell (as suggested in Microsoft documentation OR you can install via "Turn Windows features on or off")
1515
1616
1717
1818You can run control panel (press Win+R type in "control" and press enter) and search for "windows features" in order to bring the dialog up. The link is also available in "Add and Remove programs" section of control panel).
1919
2020![](/wiki-images/c/ce/Wsl_enable_marked.png)
2121
2222Note: WSL1 only requires "Windows subsystem for Linux" to be installed.
2323
2424"Virtual Machine Platform" is only requires for WSL2.
2525
2626- Install and Run Ubuntu distro
2727- Update the system
2828
2929
3030Run the following commands on Linux command-line
3131
32-`sudo apt-get update`\
33-`sudo apt-get upgrade`
32+```text
33+sudo apt-get update
34+sudo apt-get upgrade
35+```
3436
3537### Running the test app from Windows
3638
3739The "wsl" utility can be used to run Linux (x86_64) targets on Windows. If you used the example above to compile the application, then you should be able to run as following, from Windows command-line
3840
39-`cd c:\testrun`\
40-`wsl ./testrun`
41+```text
42+cd c:\testrun
43+wsl ./testrun
44+```
4145
4246
4347output
4448
45-`hello world`
49+```text
50+hello world
51+```
4652
4753### Running (linux) FPC from Windows
4854
4955Similar to the sample application, FPC itself can also be ran from Windows.
5056
5157Here's an example create a folder in Windows, and create a file there test.pas. Edit the file (i.e. using notepad) and put the following code:
5258
5359``` pascal
5460procedure RunLoop;
5561var
5662 i : integer;
5763begin
5864 for i:=1 to 10 do
5965 write(i,' ');
6066 writeln;
6167end;
6268
6369begin
6470 {$ifdef mswindows}
6571 writeln('windows');
6672 {$endif}
6773 {$ifdef linux}
6874 writeln('linux');
6975 {$endif}
7076 writeln('hello world');
7177 writeln(sizeof(Pointer));
7278 RunLoop;
7379end.
7480```
7581
7682Run the following commands from command line. Note, that the use of "WSL" is critical (otherwise you'll get a windows binary). (Running the command-line requires both WSL installed and FPC for the distro installed)
7783
78-`wsl fpc ./test`\
79-`wsl ./test`
84+```text
85+wsl fpc ./test
86+wsl ./test
87+```
8088
8189You should see in command-line the following output:
8290
83-`linux`\
84-`hello world`\
85-`8`\
86-`1 2 3 4 5 6 7 8 9 10`
91+```text
92+linux
93+hello world
94+8
95+1 2 3 4 5 6 7 8 9 10
96+```
8797
8898Indicating the binary was built for linux 64-bit system
8999
90100### Running (linux) Gdb from Windows
91101
92102WSL1 provides enough functionality in order for GDB to run.
93103
94104However, GDB itself is not installed by default (on Ubuntu 16.4 distro) and needs to be installed prior to use
95105
96-`wsl sudo apt-get install gdb`
106+```text
107+wsl sudo apt-get install gdb
108+```
97109
98110Recompile the existing sample program with Dwarf2 enabled.
99111
100-`wsl fpc -gw2 test.pas`
112+```text
113+wsl fpc -gw2 test.pas
114+```
101115
102116(you should see the size of the executable increasing)
103117
104118Run GDB for the compiledexecutable.
105119
106-`wsl gdb test`
120+```text
121+wsl gdb test
122+```
107123
108124you should see:
109125
110-`C:\testbuild>wsl gdb test`\
111-`GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1`\
112-`Copyright (C) 2016 Free Software Foundation, Inc.`\
113-`License GPLv3+: GNU GPL version 3 or later <`[`http://gnu.org/licenses/gpl.html`](http://gnu.org/licenses/gpl.html)`>`\
114-`This is free software: you are free to change and redistribute it.`\
115-`There is NO WARRANTY, to the extent permitted by law.  Type "show copying"`\
116-`and "show warranty" for details.`\
117-`This GDB was configured as "x86_64-linux-gnu".`\
118-`Type "show configuration" for configuration details.`\
119-`For bug reporting instructions, please see:`\
120-`<`[`http://www.gnu.org/software/gdb/bugs/`](http://www.gnu.org/software/gdb/bugs/)`>.`\
121-`Find the GDB manual and other documentation resources online at:`\
122-`<`[`http://www.gnu.org/software/gdb/documentation/`](http://www.gnu.org/software/gdb/documentation/)`>.`\
123-`For help, type "help".`\
124-`Type "apropos word" to search for commands related to "word"...`\
125-`Reading symbols from test...done.`
126+``` pascal
127+C:\testbuild>wsl gdb test
128+GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
129+Copyright (C) 2016 Free Software Foundation, Inc.
130+License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
131+This is free software: you are free to change and redistribute it.
132+There is NO WARRANTY, to the extent permitted by law. Type "show copying"
133+and "show warranty" for details.
134+This GDB was configured as "x86_64-linux-gnu".
135+Type "show configuration" for configuration details.
136+For bug reporting instructions, please see:
137+<http://www.gnu.org/software/gdb/bugs/>.
138+Find the GDB manual and other documentation resources online at:
139+<http://www.gnu.org/software/gdb/documentation/>.
140+For help, type "help".
141+Type "apropos word" to search for commands related to "word"...
142+Reading symbols from test...done.
143+```
126144
127145at the initial-load place a breakpoint within the runloop procedure:
128146
129-`(gdb) b test.pas:6`
147+```text
148+(gdb) b test.pas:6
149+```
130150
131151resulting in:
132152
133-`Breakpoint 1 at 0x4001e6: file test.pas, line 6.`
153+```text
154+Breakpoint 1 at 0x4001e6: file test.pas, line 6.
155+```
134156
135157and proceed with the execution:
136158
137-`(gdb) r`\
138-`Starting program: /mnt/c/testbuild/test`\
139-`linux`\
140-`hello world`\
141-`8`\
159+``` pascal
160+(gdb) r
161+Starting program: /mnt/c/testbuild/test
162+linux
163+hello world
164+8
142165
143-`Breakpoint 1, RUNLOOP () at test.pas:6`\
144-`6           write(i,' ');`\
145-`(gdb) `
166+Breakpoint 1, RUNLOOP () at test.pas:6
167+6 write(i,' ');
168+(gdb)
169+```
146170
147171at this point you can do all typical Gdb actions you need to do. I.e. researching stack:
148172
149-`(gdb) bt`\
150-`#0  RUNLOOP () at test.pas:6`\
151-`#1  0x00000000004002fa in main () at test.pas:19`
173+```text
174+(gdb) bt
175+#0 RUNLOOP () at test.pas:6
176+#1 0x00000000004002fa in main () at test.pas:19
177+```
152178
153179or terminating the exectuion
154180
155-`(gdb) q`
181+```text
182+(gdb) q
183+```
156184
157185## WSL2
158186
159187WSL2 is a different approach, that's implemented using an actual Linux kernel running in a virtual machine. Namely the entire Linux kernel functionality is available (... and the sky is limit!). Microsoft also addressed some performance issues when writing files to NTFS.
160188
161189### Installing
162190
163191- You need to have both "Windows Subsystem for Linux" and "Virtual Machine Platform" installed
164192- (Summer 2019) you need to be subscribed (it's free) for Windows Insider Preview program and use "FAST" (instead of "Slow) option of getting updates.
165193 - That will allow you to install the "preview" edition of Windows with WSL2 available.
166194- Install and initialize a desired Linux distro from Microsoft Online store
167195- Switch the distro to use version 2, by running the following command-line:
168196
169-`wsl --set-version Ubuntu-16.04 2`
197+```text
198+wsl --set-version Ubuntu-16.04 2
199+```
170200
171201
172202if you running into troubles switching to Version 2, see below.
173203
174204The process of setting a version takes about a couple of minutes so be patient.
175205
176206#### Unable to switch to version 2
177207
178208If you're getting this error
179209
180-`The requested operation could not be completed due to a virtual disk system limitation. `\
181-`Virtual hard disk files must be uncompressed and unencrypted and must not be sparse.`
210+```text
211+The requested operation could not be completed due to a virtual disk system limitation.
212+Virtual hard disk files must be uncompressed and unencrypted and must not be sparse.
213+```
182214
183215There are 2 ways you should be able to resolve this:
184216
185217- Either manually remove "Compressed" flag from the distro folder, which usually resides at:
186218
187-`%localappdata%\Packages\CanonicalGroupLimited.UbuntuonWindows_xxxxxxx`
219+```text
220+%localappdata%\Packages\CanonicalGroupLimited.UbuntuonWindows_xxxxxxx
221+```
188222
189223![](/wiki-images/f/fc/Wsl_compression_mark.png)
190224
191225- or try to disable compression and encryption for the entire disk. The following commands must be ran in "Administrator" mode (and would require a reboot)
192226
193-`fsutil behavior set disableencryption 1`\
194-`fsutil behavior set disablecompression 1`
227+```text
228+fsutil behavior set disableencryption 1
229+fsutil behavior set disablecompression 1
230+```
195231
196232See more at: <https://github.com/microsoft/WSL/issues/4103>
197233
198234If you're getting an error of Timeout - try to restart the PC.
199235
200236### Using FPC
201237
202238The process of installation and use of FPC is identical to the described in WSL1. (The installation isidentical to an actual Linux or Linux running in a 3d party Virtual Machine)
203239
204240No additional changes are needed for FPC itself in order to run and compile from WSL2.
205241
206242### Using GDB
207243
208244In general the installation of GDB is similar to WSL1.
209245
210246It seems like there were changes in pipe handling in Gdb, compared to WSL1.
211247
212248Thus running
213249
214-` wsl gdb application_name`
250+```text
251+ wsl gdb application_name
252+```
215253
216254would still work, but sometimes pipe communication might fail and you would not see a GDB prompt.
217255
218256On the other hand, if a user tries to use gdb directly from wsl.exe shell, it works just fine.
219257
220258## Running ELF binaries from Windows app
221259
222260Any windows application can run ELF binary using "wsl.exe".
223261
224262Windows itself would not recognize ELF executable as an executable. Thus an attempt to open it would make Windows prompt the user for the host application to be specified. There's not special Windows API yet to run them. So far the only way of running an ELF binary is using "wsl.exe".
225263
226264That means that a desired Linux distro(s) needs to be installed. (and preferably one of them selected as default)
227265
228266### Using WSL.exe
229267
230268Running ELF binary with WSL.exe requires an explicit call to "wsl.exe" and passing ELF binary as a parameter, as well as any additional parameters.
231269
232270That typically involves creating a process with StdOut/StrErr redirected to pipes (either by using WinAPI directory or [[TProcess]]).
233271
234272**Caveat**: there seems to be some special handling of Pipe-redirected output in WSL itself. The launched WSL might not terminate until it receives something on the input buffer. It might also skip some output, if the launching application is "sleeping" for too long. Thus the typical wait for output period should be minimal.
235273
236274``` pascal
237275 p := TProcess.Create(nil);
238276 try
239277 p.Executable := 'wsl.exe';
240278 p.Parameters.Add('ls'); // running ls utility
241279 p.Parameters.Add('-l'); // with -l parameter
242280 p.Options := [poUsePipes];
243281
244282 while (p.Running) or (p.Output.NumBytesAvailable > 0) do
245283 begin
246284 if p.Running and (p.Output.NumBytesAvailable = 0) then
247285 begin
248286 s:=#0; // writing anything to the input
249287 p.Input.Write(s, length(s));
250288 Sleep(1); // the shortest sleep possible
251289 end;
252290
253291 sz := p.Output.NumBytesAvailable;
254292 if sz>0 then begin
255293 SetLength(s, sz);
256294 sz := p.Output.Read(s[1], sz);
257295 if sz < length(s) then SetLength(s, sz);
258296 write(s);
259297 end;
260298 end;
261299
262300 finally
263301 p.Free;
264302 end;
265303```
266304
267305It also doesn't seem that WSL is returning the exit code back.
268306
269307One can refer to the full code example of "wslwrap" application: <https://github.com/skalogryz/wslwrap>
270308
271309## Lazarus
272310
273311### Using Lazarus from WSL
274312
275313WSL itself comes without graphical support by default. Yet it should be possible to install X-Windows server on Windows 10 machine itself, and configure WSL distro to use Windows 10 as its windows server.
276314
277315That also should open an option for developing and debugging Linux GUI applications using WSL.
278316
279317### Using WSL FPC and GDB from Windows
280318
281319As long as Lazarus is capable of "prefixing" every FPC (and Gdb) commands with "wsl" - forcing the Windows Subsystem for Linux to execute, it should be capable of producing native (command-line) executables.
282320
283321It's likely Lazarus on Windows would also need to substitute the proper file path when dealing with WSL-ed projects.
284322
285323### Quick and Dirty - Using WSL FPC from Windows
286324
287325**Dislaimer**: the proper solution might require additional work on IDE side. While the compiler doesn't need to be changed at all. It's possible to create a WSL compiler in order to by-pass Lazarus limitations, without a need of building a special cross-compiler.
288326
289327- before begin, make sure you've FPC installed on WSL (linux) side.
290328- Build wslwrap application (https://github.com/skalogryz/wslwrap)
291329- Rename wslwrap to ppcwsl_fpc.exe (a name can also follow FPC cross-compiler name convention, in this case the result might be even better)
292330
293331
294332The such name has two purposes:
295333
2963341\) it starts with "ppc". Lazarus verifies the compiler executable to start with either "ppc" or "fpc", otherwise it would produce (a lot of annoying) warnings
297335
2983362\) "_fpc" part enforces the use of "fpc" executable on WSL side (this is "wslwrap" feature)
299337
300338- Place the executable to your lazarus fpc configuration folder. (next to the current fpc.exe)
301339- In Lazarus IDE options change "Compiler executable" to the new ppcwsl_fpc.exe.
302340
303341
304342The IDE would complain about "fpc.cfg" missing - ignore it.
305343
306344![](/wiki-images/6/65/Wsl_fakefpc_ideoption.png)
307345
308346- Start a new (non GUI) project, i.e. with the following code
309347
310-`begin `\
311-`  writeln('hello world');`\
312-`end.`
348+``` pascal
349+begin
350+ writeln('hello world');
351+end.
352+```
313353
314354- Change project settings to:
315355
316356
317357Target OS = Linux
318358
319359Target CPI = x86_64
320360
321361- Compile the project
322362- In the project folder you should see the ELF executable showing up. You should be able to execute it using "wsl.exe"
323363
324364![](/wiki-images/6/65/Wsl_widows_compiled.png)
325365
326366In order to rollback, simply restore the original FPC configuration in the IDE option.
327367
328368## See Also
329369
330370- <https://docs.microsoft.com/en-us/windows/wsl/about> - official Microsoft WS documentation
331371- <https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux> wikipedia
332372- <https://virtualizationreview.com/articles/2017/02/08/graphical-programs-on-windows-subsystem-on-linux.aspx-> running WSL programs using X-Windows server installed on native Windows 10.
333373
334374---
335375*From the Free Pascal wiki, page "Windows Subsystem for Linux", as it stood on 2019-12-14 after a change by Bart, 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).*
336376Category: Linux
337377Category: Windows

Back to history