-
Notifications
You must be signed in to change notification settings - Fork 835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to detect if running under WSL? #4071
Comments
I think your question should be addressed to the gcc or clang compiler team not WSL |
The Windows version is present in our kernel command line (/proc/version, or uname -a). |
if [[ $(grep Microsoft /proc/version) ]]; then |
Thanks for the comments. I would like to detect WSL from a preprocessing directive when compiling a program. Something like #ifdef __ WSL __. There are special versions of gcc for Mingw and Cygwin, but as I understand it, there is no special version of gcc for WSL. Is there any way that a __ WSL __ define can be added automatically when the compiler is running under WSL? |
@AgnerF it is a kind of define that you can do yourself. You don't need compiler support for that. Just generate config.h file using the build system of your choice. Here is how to do it in meson for example:
config.h.in file
Meson will generate
|
Is there a way to determine WSL1 vs WSL2. I normally use the |
@AgnerF the idea here is that unmodified Linux binaries run on WSL (and that binaries compiled on WSL are unmodified Linux binaries), so you’ll have to do a run-time check, not a compile-time check. |
A little correction/update for what @xtremeperf said, you need to grep case insensitive. On my (maybe newer?) WSL, it is not a capital M, just "microsoft" so use this for greater confidence.
|
@flickerfly its actually WSLv2 using lowecase from my experience. If Ya'll are interested here's what I use to detect all operating environments that I use.
|
Josiah Ritchie dixit:
A little correction/update for what @xtremeperf said, you need to grep
case insensitive. On my (maybe newer?) WSL, it is not a capital M, just
"microsoft" so use this for greater confidence.
```
if [[ $(grep -i Microsoft /proc/version) ]]; then
echo "Bash is running on WSL"
fi
```
Define “maybe newer?”, because I have this:
/* check uname first (this is improbable to fail) */
if ((uerr = uname(&u)) == 0) {
if (strstr(u.release, "Microsoft") != NULL) {
/* pretty certainly WSL 1 */
errno = e;
return (1);
}
if (strstr(u.release, "microsoft") != NULL) {
/* probably WSL 2 */
errno = e;
return (2);
}
}
This distinction is important (in this library, anyway).
I have access to a system with both WSL 1 and 2, but that
system is not going to be updated to Win11, so I might
miss out on latest changes. (For networking, WSL 1 is
better anyway.)
bye,
//mirabilos
--
Infrastrukturexperte • tarent solutions GmbH
Am Dickobskreuz 10, D-53121 Bonn • http://www.tarent.de/
Telephon +49 228 54881-393 • Fax: +49 228 54881-235
HRB AG Bonn 5168 • USt-ID (VAT): DE122264941
Geschäftsführer: Dr. Stefan Barth, Kai Ebenrett, Boris Esser, Alexander Steeg
|
The difference changes between WSLv1 and WSLv2 hence why I have the distinction in the script I provided above your comment. |
b-hayes dixit:
The difference changes between WSLv1 and WSLv2
Could you please explain that? What exactly changes?
Used to be (?) that
• "Microsoft" is WSL 1 with a very high probability
• "microsoft" is WSL 2 with good probability
• if uname contains neither, check environment variables
– WSL_INTEROP is relatively certainly WSL 2
– WSL_DISTRO_NAME is WSL; if uname check succeeded, WSL 1 was
already caught above, so it’s WSL 2; if uname failed guess
WSL 1 instead since WSL 2 would (also) have WSL_INTEROP set
Is this no longer correct?
bye,
//mirabilos
--
Infrastrukturexperte • tarent solutions GmbH
Am Dickobskreuz 10, D-53121 Bonn • http://www.tarent.de/
Telephon +49 228 54881-393 • Fax: +49 228 54881-235
HRB AG Bonn 5168 • USt-ID (VAT): DE122264941
Geschäftsführer: Dr. Stefan Barth, Kai Ebenrett, Boris Esser, Alexander Steeg
|
That's correct. The difference in case changes from WSL to WSLv2. As per my script above, WSLv2 uses lowercase. |
b-hayes dixit:
That's correct.
Huh, why did you write “The difference changes” then?
The difference in case changes from WSL to WSLv2. As
per my script above, WSLv2 uses lowercase.
Do you maybe mean the difference *is* in the changed case?
|
Yeah, that's what I meant sorry for the confusion. |
is there anyway to detect if running under WSL but for java jar runtime? |
@cuynu you could run a shell command in Java and use the same approach as my script does: #4071 (comment) |
this needs to be |
I think this should be transferred to a https://github.com/microsoft/WSL/discussions/new?category=q-a. |
How do you detect if running under WSL?
C compilers in Mingw and Cygwin have preprocessing defines that allow you to identify the system. Is there a WSL preprocessing define or some other method for identifying the system? If not, please make a preprocessing define that gives a WSL version number.
You may claim that this is a genuine Linux (Ubuntu or whatever), but it is not. The floating point control word is set to reduced precision, and this causes a lot of problems. (Why has issue #830 never been fixed?). Also, of course, there is no GUI in WSL.
The text was updated successfully, but these errors were encountered: