Setting Up C in VSCode IDE

It is another Sunday morning in the Twin Cities of Minneapolis and St. Paul. The weather forecast indicated that we would be receiving about 3 inches of fresh snow. It seems that we will be getting about 0.1 inches of new accumulation. Given that today is February 28, in the next couple months we might still get winter storms bringing 6 or more inches each.

This past week I had a very nice and interesting conversation with a Principal Software Architect at one of the largest software companies in the world. It was very educational. I really enjoyed the conversation. One of the things that initially called my attention was when I asked the kind of things he does on a regular basis. He mentioned that he “provides guardrails” among other things. When he mentioned it I was not sure what he meant so I made a note to ask him later. At some point I had a chance to describe what I do and have done. One of the things I mentioned was sets of DLLs that with time I have created with the purpose to help developers use a set of tools to get some routine work done without having to redesign and test code use as a scaffold and be able to concentrate on the task at hand. Among the libraries that I have developed are some that deals with encryption, linked lists (I refer to them as queues), sockets, trees, etc. I was then clarified what “guardrails” refer to. It is the same concept in order help developers produce faster code with fewer bugs. It seems that we are performing some similar tasks.

I did a search on the web on “software guardrails” to find out more about the topic. I found and read some of the associated articles. For example the article “Guardrails Are Essential for Any Distributed Agile Team” by Sharlene McKinnon lists some additional items. The general idea is the same and is very well expressed in the following snippet:

“The objective of any Agile leader is to facilitate the definition and building of these guardrails so teams can be successful. This definition is the foundation of high-performing teams. And the more robust the foundation, the stronger the teams.”

I know of several Agile teams, at different companies, that should read the article and start using the ideas presented by Sharlene McKinnon.

I have mentioned in the Design Linked List post in this blog, that I had ordered s et of books on Algorithms in C and C++ by Robert Sedgewick. I finished reading the first chapter and was about to start experimenting with the contents of that chapter when I noticed that I have not added support for C / C++ in my copies of VSCode IDE. I could have used Visual Studio 2019 Enterprise Edition which I use for work, but I decided that it would be best if I could use VSCode with a couple more programming languages. Earlier this morning I installed the necessary prerequisites and now I am able to compile C software using VSCode.

The prerequisites are:

o Install MinGW (https://en.wikipedia.org/wiki/MinGW) C and C++ toolset.

o Install Visual Studio Code (I have done this a couple years ago).

First you need to Download and Install C / C++ toolset (i.e., compiler and linker) i.e., MinGW.

Second you need to install Visual Studio Code.

Finally that you need to install a couple extensions to run C and C++ programs in VSCode.

I visited the SOURCEFORGE MinGW-w64 – for 32 and 64 bit Windows page and downloaded the version for my Windows 10 64-bit machine.

I downloaded the installer to the following location on my computer:  C:\temp\mingw-w64-install.exe

After the download completed I ran the installer.

I selected the Architecture:  x86_64

The software was installed at the following path:  C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0

When done I add to PATH:  C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin

To make sure all was well I issued the following commands:

# **** get the version of the compiler ****
C:\>g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 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.

# **** get the version of the debugger ****
C:\>gdb --version
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".

I did read the article Using GCC with MinGW which provides instructions on how to compile and run C / C++ code. I believe that since C is an older programming language additional steps were needed. I have installed Java and Python on VSCode and as far as I can recall, making a program run and accept input and generate output required less steps.

When I was done with the launch.json file it looked as follows:

{
    // 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": "gcc.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe build active file"
        }
    ]
}

The tasks.json file looks as follows:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: gcc.exe build active file",
			"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe\""
		}
	]
}

I then wrote a simple test to make sure I could display and get input from the console.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


/**
 * First C program in VSCode.
 */
int main(int argc, char const *argv[])
{

    // **** initialization ****
    int  response;

    // **** display message ****
    printf("main <<< !!! First C program in VSCode !!!\n");

    // **** prompt user for input ****
    printf("main >>> enter integer: ");
    scanf("%d", &response);

    // **** display response ****
    printf("main <<< you entered: %d\n", response);

    // **** all done ****
    return 0;
}

The program displays a message followed by a prompt. After the user enters an integer value and presses <Enter> the problems should display the value and exit. I tested the program using the following commands:

Run Code (Ctrl+Alt+N>
Run -> Run Without Debugging (Ctrl+F5)
Run -> Start Debugging (Ctrl+F11)
F5

The program started and worked well.

main <<< !!! First C program in VSCode !!!
main >>> enter integer: 2021
main <<< you entered: 2021

This is a sample run of the test code. I entered 2021 at the prompt followed by <Return> and the program displayed the same value.

I am ready to experiment and generate a post tomorrow.

Hope you enjoyed solving this problem as much as I did. The entire code for this project can be found in my GitHub repository.

If you have comments or questions regarding this, or any other post in this blog, or if you would like for me to help out with any phase in the SDLC (Software Development Life Cycle) of a project associated with a product or service, please do not hesitate and leave me a note below. If you prefer, send me a private e-mail message. I will reply as soon as possible.

Keep on reading and experimenting. It is one of the best ways to learn, become proficient, refresh your knowledge and enhance your developer toolset.

One last thing, many thanks to all 7,082 subscribers to this blog!!!

Keep safe during the COVID-19 pandemic and help restart the world economy. I believe we can all see the light at the end of the tunnel.

Regards;

John

john.canessa@gmail.com

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.