Page 1 of 1

WSL2 - Install Linux C/C++ Compilers

Posted: Tue Jun 23, 2020 8:54 am
by jussij
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:

Code: Select all

wsl.exe --list
This command will show a listing similar to the following:

Code: Select all

Windows Subsystem for Linux Distributions:
Ubuntu-24.04 (Default)
docker-desktop
docker-desktop-data
The default distribution can be set using the following command, changing the distribution name to suit:

Code: Select all

wsl -s Ubuntu-24.04
2. Open the newly installed WSL2 distribution and at the resulting command line update the packages using this command:

Code: Select all

sudo apt update
3. Install the build-essential package using this command:

Code: Select all

sudo apt install build-essential
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:

Code: Select all

sudo apt-get install manpages-dev
5. To test the C compiler installation run this command:

Code: Select all

gcc --version
That command should result in this output:

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.
6. To test the C++ compiler installation run this command:

Code: Select all

g++ --version
That command should result in this output:

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.
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:
wsl-compile.png
wsl-compile.png (134.46 KiB) Viewed 351 times
The compiler command line used is as follows:

Code: Select all

g++ "$f" -g -o "$fb"
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:

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;
}
Assume that file lives in the following Windows location:

Code: Select all

C:\Projects\c++\test.cpp
Inside the WSL Linux terminal this will map to the following location:

Code: Select all

/mnt/c/Projects/c++/test.cpp
After running the compile that directory will now contain these files:

Code: Select all

test  test.cpp
The test is the executable and it can be run in the WSL terminal using the following command:

Code: Select all

./test
Running that executable produces the following output:

Code: Select all

Hello World!

Argument: 0 => ./test
This executable can also be run inside of Zeus using the Tools, Bash Command Line menu which will display this dialog:
wsl_run.png
wsl_run.png (17.86 KiB) Viewed 349 times
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:
wsl-output.png
wsl-output.png (3.53 KiB) Viewed 349 times