Tag: msvc

In praise of Visual Studio

In praise of Visual Studio

Visual Studio debuggingLooking at some of the posts on the C subreddit about recommendations for a text editor to help learn C, I see many people recommending editors like Sublime Text, Notepad (on Windows).

I started programming in an era when IDEs were few and far between and an IDE is a wonderful time saving device. It is vastly superior to editing in a text editor then launching a compiler either by command or a batch file.

With an IDE you get immediate feedback on where the errors are, sometimes with a mini-map that lets you navigate directly to the error. It’s quicker to refactor code (though that’s a function of an editor) and usually one key press away from running or debugging.

Debugging in particular is very good in Visual Studio on Windows or Visual Studio Code on Windows/Linux/Mac. It’s visual, you can see multiple lines of code, add breakpoints and step through it with one key press.

I find line debuggers like GDB to be very frustrating by comparison.  Mousing over a variable and seeing its value in a debugger is a big time saver. Also seeing the stack calls and being able to jump to calls in the stack is handy.

Now if you are used to Linux, working in a terminal and compiling code through batch files then fine, but I believe its a bit of a hurdle for new programmers. An IDE makes it easy to switch from editing, compiling and debugging without leaving the program. It is all round more productive and easier to get into for a beginner.

So it’s worth at least trying out the free version (Community Edition) of Visual Studio if you are on Windows.  The days when Microsoft hated Linux (They called it a cancer!) are long past.  And no I don’t get paid for advocating Visual Studio; I just like it. Considering that it’s completely free, it’s remarkable value.

Comparing MSVC vs Clang

Comparing MSVC vs Clang

Listing of some C codeI originally created Asteroids for Windows using Visual Studio 2017 Community Edition. Since then I’ve started the Clang version on Ubuntu and there haven’t been too many differences but there are just a few so in this post I’ll list what I’ve found so far.

Include Paths

On Windows, I was able to get away with #include <SDL.h> but on Linux, I’ve had to include the path so it’s #include <SDL2/SDL.h>. This was probably because I included the full path in the MSVC configuration.

Link Failures

The asteroids.c code in chapter 29 uses sin and cos for the first time and the linker was unhappy with that. So in tasks.json, I’ve explicitly had to add it into args, along with SDL2 and SDL2_image,

            "args": [
                "-g",
                "${file}","${workspaceFolder}/Asteroids/hr_time.c",
                "-o",
                "${fileDirname}/asteroids",                
                "-lSDL2",
                "-lSDL2_image",
                "-lm"
            ],

That “-lm” does that for maths.

Safe functions

Microsoft has its own set of safe functions many with an _s and extra length parameter.

On Linux, there don’t seem to be so many.

So sprintf_s on Windows becomes snprintf.

fopen_s just becomes standard fopen

linux/time.h

As well as time.h in the includes, I needed to add linux/time.h as well.