Installing and using Visual Studio Code and g++ (plus other tools) for programming (re)learning and entertainment purposes (Part 1)
Trying to solve one of Hackerrank problems with Visual Studio Code

Installing and using Visual Studio Code and g++ (plus other tools) for programming (re)learning and entertainment purposes (Part 1)

NOTE (2019-11-17): Since I posted this article, both WSL and VSC had greatly improved.
It is possible now to run Visual Studio code (and other graphical interface programs) inside WSL (by installing XFCE and with the help of and X Server) or, better, to use remote development (see more at https://code.visualstudio.com/docs/remote/remote-overview).

See also the next instalments ( Part2, Part 3) of this article
See a slightly updated version of the series in my GitHub ( https://github.com/Adrian-LL/vscode-cpp-windows).

Recently I wanted to refresh my programming (and logical) skills. So I thought to try some available tools. I was not looking for professional tools, but for something - an IDE - easy to use, ideally with a lot of help included. (still longing for something like the almost forgotten Turbo C and Turbo Pascal.

I tried (and still using) CodeBlocks (https://www.codeblocks.org/ - works on Linux, Windows, MacOS). For educational purposes it is very simple to setup, just download and install the version that contains the mingw tools. More details on their site. It is also easy to use, almost straightforward. However, it can also deal with bigger projects.

I also tried NetBeans (https://netbeans.org/ - also working in Linux, Windows and MacOS) and DevC++ (https://sourceforge.net/projects/orwelldevcpp/ - Windows only), plus a short stint with CLion (which I did not liked - but that's a personal thing). Speaking of which, I will not discuss about full-fledged tools such Visual Studio (available free in the Community Edition, https://www.visualstudio.com/vs/community/, for Windows and MacOS) or Xcode for MacOS, as they seemed too complicated for my purpose. BTW, Visual Studio is huge and I was forced to delete it from my laptop as I did not had enough disk space available after some time.

Please note there are a lot of very good (code) editors including Notepad++ (https://notepad-plus-plus.org/), atom (https://atom.io/), Sublime Text (https://www.sublimetext.com/) or the ever-faithful vim (https://www.vim.org/) and emacs (https://www.gnu.org/software/emacs/). There are even code editors in Microsoft Store (such as Code Writer https://www.actiprosoftware.com/products/apps/codewriter - which may not be a bad idea).

As I saw a lot of hype lately, I wanted to give a chance to Visual Studio Code (https://code.visualstudio.com/). Keep in mind though, although Code can be used for "real" projects, I will refer below mainly to my experience for educational purposes (i.e learning, compiling small console programs and so on.)

Steps I used (the information is already available on the internet, but sometimes there is *too much* information).

  • Download & install Visual Studio Code (this can be also done later, after the next step.)
  • You may get an error about git, let's ignore it for the moment.
  • A development environment / set of tools is needed (that is, compiler, debugger, libraries…) but for my needs an open-source "toolchain" is enough - usually the MinGW is most recommended (and you can download it from here: https://sourceforge.net/projects/mingw/). Very familiar for those with Linux experience.
Note however, that the programs resulted are not totally "portable" as they are dependent on some libraries.
  • Based on my trials, MinGW is simple to install, but some tools are missing or not working as they should from the default installation (e.g. make or clang). The installer is ok(ish), but the process of selection is tedious. The uninstall is a little tricky (even while is not actually "installing" something but rather creating some folders and extracting some files there) - but basically means deleting some folder(s) and files.
(Another parenthesis: the fact that there are multiple versions of MinGW, and the discussion about what is Cygwin vs MinGW vs MSYS will be left for some other time… see for example here https://github.com/msys2/msys2/wiki/How-does-MSYS2-differ-from-Cygwin)
  • So, a little more complicated, but somewhat better is the MSYS2 distribution, as it contains MSYS2, MinGW-64 & 32 and also Qt5. Available also is Clang. This will need more fine tuning as follows:
  • Download it from https://www.msys2.org/. You will find there also the installation instructions. See also the instructions from https://github.com/msys2/msys2/wiki/MSYS2-installation with important remarks about folder names and slightly better pacman commands.
  • Please keep the default path for installation (i.e. C:\), even try to shorten it as much as possible. For example, C:\msys64 or C:\MSYS - see more in the link above. Just hit "Next".
  • It will ask to launch the console, proceed to it. Otherwise look from the msys2_shell.bat in the MSYS2 base directory (that chosen above).

Now we have to actually install the packages needed for programming.

  1. Run pacman -V in the console (this is the same pacman package manager form Arch Linux). If the version is smaller than 5.0.1 run "update-core" first.
  2. Launch pacman - Syuu.
  3. You will get some errors about catgets - choose Y.
  4. At some moment the console will freeze as it tries to update itself. Just close it (from Windows) and launch again until it does nothing at pacman -Syuu. The update will take a while. The "freeze" may happen also when you do periodical checks for updates (when updating bash or some base libraries)
  5. Install the build packages.
  • The first one is pacman -S base-devel. There will be over 70 packages and more than 200 MB. Just choose "all". This will install basic tools and libraries needed for basic development (you will have perl and python included, also (g)awk, bison and so on.)
  • Then install pacman -S mingw-w64-x86_64-toolchain for building mingw64 packages. (you can query - see below - and choose the package name). This will have like 40 packages and over 700 MB (deflated). There will be some overhead as not all of them are really needed.
  • Optionally you can install pacman -S mingw-w64-i686-toolchain for building mingw32 packages.
  • Also, optionally CLANG (optional search first with pacman -Ss clang) pacman -S mingw64/mingw-w64-x86_64-clang or Cmake (pacman -Ss cmake), pacman -S mingw64/mingw-w64-x86_64-cmake

Some pacman commands useful for General Package Management

  • Query about the existence of some package: pacman - Ss <part_of_package_name> It will indicate the correct name for the package and also if it's installed or not. For example, pacman -Ss gcc
  • Installing new packages: pacman -S <package_names|package_groups>
  • For example, pacman -S make gettext base-devel In this example is a package group which contains many packages. If you try to install a package group, Pacman will ask you whether you want to install one package from the group or all of the packages from the group.
  • Removing packages: pacman -R <package_names|package_groups>
  • Searching for packages: pacman -Ss <name_pattern>

More on our Using packages wikipage.

Beware that mixing in programs from other MSYS2 installations, Cygwin installations, compiler toolchains or even various other programs is not supported and will probably break things in unexpected ways. Do not have these things in PATH when running MSYS2 unless you know what you're doing. ( From < https://github.com/msys2/msys2/wiki/MSYS2-introduction > )

After MSYS installation:

  • Modify the PATH system variable in Windows (through "Edit the system variables" in Control Panel - you can edit it system-wide or per-user) to contain the paths for MSYS and MinGW installations.
  • Launch cmd.exe and verify the paths by running gcc -V and g++ -V (you can try also gbd, make, cmake, etc.)
  • If you din not already installed Visual Studio Code, install it now.
  • IMPORTANT - Install C/C++ (Microsoft) extension (latest version was 0.17.1).
(This is not a manual for VSC so don't look for detailed explanations)

Modify the files needed

  • tasks.json
  • launch.json
  • cpp_properties.json

See bibliography below:

For example, you may have to modify the include paths for intellisense purposes in VSC cpp_properties.json (NOTE - since the latest update of VSC 1.23.1 and C/C++ extensions I found that the update of that file was not needed.). But see https://github.com/Microsoft/vscode-cpptools/blob/master/Documentation/Getting%20started%20with%20IntelliSense%20configuration.md.

My tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile C++",
            "type": "shell",
            "command": "g++ -g ${file} -o ${fileBasenameNoExtension}",
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

My launch.json (works well enough for debugging purposes)

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

There are better configurations though :-)

Note the use of forward slashes "/" in the paths specifications above - VSC recognizes them. Otherwise one can put escaped backslashes "\\".

Short remainder of the variables names - see more at https://code.visualstudio.com/docs/editor/variables-reference

${workspaceFolder} - the path of the folder opened in VS Code
${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
${file} - the current opened file
${relativeFile} - the current opened file relative to workspaceFolder
${fileBasename} - the current opened file's basename
${fileBasenameNoExtension} - the current opened file's basename with no file extension
${fileDirname} - the current opened file's dirname
${fileExtname} - the current opened file's extension
${cwd} - the task runner's current working directory on startup
${lineNumber} - the current selected line number in the active file
${selectedText} - the current selected text in the active file
Note: The ${workspaceRoot} variable is deprecated in favor of the ${workspaceFolder} variable.


Enjoy!
To be continued

What is your opinion?

       

要查看或添加评论,请登录

Adrian Liviu LUDOSAN的更多文章

社区洞察

其他会员也浏览了