WSL2 - Install Linux C/C++ Compilers
Posted: Tue Jun 23, 2020 8:54 am
1. Make sure WLS2 has been installed on the machine. For example, see the details found here: https://phoenixnap.com/kb/upgrade-wsl-to-wsl2
Use the following wsl.exe command to check a Linux distribution has been installed and is set as active:
This command will show a listing similar to the following:
The default distribution can be set using the following command, changing the distribution name to suit:
2. Open the newly installed WSL2 distribution and at the resulting command line update the packages using this command:
3. Install the build-essential package using this command:
This command will install everything to run the gcc, g++ and make tools.
4. Optionally you can install the manual pages for these tools using this command:
5. To test the C compiler installation run this command:
That command should result in this output:
6. To test the C++ compiler installation run this command:
That command should result in this output:
Running these Compiler from Inside Zeus
The Zeus compiler and tool configurations have an option to run the command using Bash. This equates to running the compiler or tool from inside the Linux subsystem.
This means you can run the Linux compilers inside Zeus and have the output captured in the editor.
For example, the image below shows the compiler settings required: The compiler command line used is as follows: That command line is expanded used the details (file name and file base name) of the currently active file.
Testing the Zeus Compiler Setup
Consider a test.cpp that contains the following code:
Assume that file lives in the following Windows location:
Inside the WSL Linux terminal this will map to the following location:
After running the compile that directory will now contain these files:
The test is the executable and it can be run in the WSL terminal using the following command:
Running that executable produces the following output:
This executable can also be run inside of Zeus using the Tools, Bash Command Line menu which will display this dialog:
With the test.cpp as the active file and using the inputs shown, this will then result in the following output being captured inside of Zeus:
Use the following wsl.exe command to check a Linux distribution has been installed and is set as active:
Code: Select all
wsl.exe --list
Code: Select all
Windows Subsystem for Linux Distributions:
Ubuntu-24.04 (Default)
docker-desktop
docker-desktop-data
Code: Select all
wsl -s Ubuntu-24.04
Code: Select all
sudo apt update
Code: Select all
sudo apt install build-essential
4. Optionally you can install the manual pages for these tools using this command:
Code: Select all
sudo apt-get install manpages-dev
Code: Select all
gcc --version
Code: Select all
gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Code: Select all
g++ --version
Code: Select all
g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The Zeus compiler and tool configurations have an option to run the command using Bash. This equates to running the compiler or tool from inside the Linux subsystem.
This means you can run the Linux compilers inside Zeus and have the output captured in the editor.
For example, the image below shows the compiler settings required: The compiler command line used is as follows:
Code: Select all
g++ "$f" -g -o "$fb"
Testing the Zeus Compiler Setup
Consider a test.cpp that contains the following code:
Code: Select all
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello World!" << std::endl << std::endl;
for (int i = 0; i < argc; ++i)
{
std::cout << "Argument: " << i << " => " << argv[i] << std::endl;
}
return 0;
}
Code: Select all
C:\Projects\c++\test.cpp
Code: Select all
/mnt/c/Projects/c++/test.cpp
Code: Select all
test test.cpp
Code: Select all
./test
Code: Select all
Hello World!
Argument: 0 => ./test