How to set up Visual Studio Code for C programming?

in #programming6 years ago

I love VS code. That's the only good thing Microsoft did correctly over so many years. To be exact, I don't remember Microsoft doing anything good since Windows XP. Probably Office 2016, I like how it looks with the smooth animations as I type. If you want to know how good it is, just do a quick search on Google with the term "visual studio" and you will be amazed that Visual Studio Code comes above Visual Studio 2017. There are also a big bunch of people saying that any that that requires them to use something more complicated than Visual Studio Code is a bad day for them - simplicity done right. I mean, who does not like a code editor that does not kill computer resources and probably prevent a few programmers from murdering their laptops anyway?

It's my go-to IDE for Python and sometimes Node (don't look at me like that, I do know Node and basic JavaScript, just don't expect me to write something useful with it). When my school needs us to do C programming, I was used to use Code::Blocks, but it is sometimes buggy. Plus, it looks a little ugly. I can't live without dark themes. So I looked back at Visual Studio Code - I tried setting it up for C programming once, but ended up rage-quitting because I am a little too stupid for it. But after tinkering for some time (it happened a few months back, so I am not that sure how much time I took, but it should not take more than 1 week I guess?), I finally got it working without automatic compilation, which I don't care because all I do is single file projects required by school assignments. It comes with a integrated terminal so I just use it to summon GCC and compile my stuff then run them on the spot. But when you ask me how to set it up so that it works like that, it will take me a good amount of time to recall back what I have did to get this.

But, recently my friend finally quit programming on https://repl.it. I mean, it's a good site. I use it from time to time. But, having our productivity getting murdered by unstable internet connection and sometimes bugs on the site is just frustrating. And, he is reluctant to use Code::Blocks, for whatever reason. He told me about autocomplete (which I can of course turn it on Code::Blocks with one switch flip, but whatever). During last programming class, he agreed to try out Visual Studio Code. I agreed to help. That's the moment I finally realized how did I set up this thing for C programming.


Visual Studio Code is like a super advanced version of Vim - well, you can say that to any IDE in the world, but it is a little different. It is really a bare-bone code editor - it comes with literally nothing and you have to spoon-feed it literally everything. Setting it up for Python and Node is easy, because you only need to install their extensions, and bam, it's done. The extensions will automatically search for the interpreter and put everything in place, you don't even need to care. But for C...uh...

First of all, it does not come with a compiler. So, we are going to install one. If you are lazy, you can go and take TCC and only support ANSI C. We are learning C11, so while this would work, most fancy stuff might not. So, we have to install MinGW for Windows for the C compiler. After installing it on the PC, you will have to tick mingw32-base to mark it for installation, then go to the installation menu to press "Apply changes". It's somehow similar to how the Synaptic Package Manager for Debian works...yea, even the icon looks similar.

I'm not sure if this is the expected result or it's my issue, but the installation target directory is not added to the PATH system environmental variable upon installation. That means, you cannot happily drop yourself in any terminal and gladly summon GCC to compile your code. The fix? Search for "Edit the system environmental variables" in the start menu (this is for Windows 10, should work for Windows 7? Or just searching "path" will do, too). Go to Environmental Variables, select Path, press Edit, New, and add "C:\MinGW\bin" to it. This is assuming that you installed it to the default installation path. Well, if you have to read this, probably you didn't tweak the installation path anyway.

Now here comes the fun part.


Like I said, Visual Studio Code is actually pretty stupid - it does not care most stuff and hence you will need to tell it almost everything, like where to find your #include files, etc. That is required so that autocomplete works. Well, I can surely work without autocomplete, but using a code editor without autocomplete just feels off, right? It all happens in a config file.

You will have to create a folder named ".vscode" in the root directory of the code folder, and probably create a file called "c_cpp_properties.json" in it. There should be a way to let Visual Studio Code generate one for you, but I forgot how. In it, you will have to tell Visual Studio Code where are your library files so it can autocomplete the nice little printfs and scanfs for you. The snippet of code somehow looks like this -

The trick is to tell VS Code where are your compiler's header files are in the includePath section. To be honest, this is not the complete list yet. I have a few constants that give syntax errors but run correctly (like CLOCKS_PER_SEC), so they are not included in the list yet. Whatever, I don't really use them all the time. This is somehow the most tricky part of the entire set up, because most of the times you will spend a ton of time digging in various folders trying to find out where are the header files...well, I saved you some time in the screenshot above. It's more or less there. Good luck.

The good thing here is, this file is more or less portable unless a different version of compiler is installed. I just copied it for my friend and it works flawlessly, so you know what to do if you plan to switch workspace folders. Just clone this folder into that one and things will continue to work properly.

Lastly, there's still one more thing. I mean, after the above step you will have everything done and will be able to enjoy autocompletes and gcc from the integrated terminal to directly run your code whenever you want (hint: use Ctrl + ` to summon the integrated terminal). But, I don't know who is the legend that designed Powershell. It is so slow that I might slap them no matter who they are...whatever, you can tell VS Code to use cmd instead. Go to File -> Preferences -> Settings, and paste this thing in the User Settings panel.

{
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe"
}

There. After a relaunch, the integrated terminal will feel at least 100x faster. Well, sure, you might prefere Powershell. If that's the case, ignore this step.


Now after writing that much I somehow wonder how did I went through all these to get the feature working. Well, probably I was bored during that time. But yea, there should be a slightly less ugly way to do so, but I don't know how. As long as it works, it works. :P

If you are reading this to know how to play with this editor for your schoolwork, go for it and ask any questions you encounter :) but remember, Google is your friend. Probably you can find a video tutorial on YouTube instead.

Good luck and thanks for reading, see you next time :3

--Lilacse

Sort:  

This post has been curated by TeamMalaysia Community :-

To support the growth of TeamMalaysia Follow our upvotes by using steemauto.com and follow trail of @myach

Vote TeamMalaysia witness bitrocker2020 using this link vote for witness

Coin Marketplace

STEEM 0.32
TRX 0.12
JST 0.034
BTC 64664.11
ETH 3166.18
USDT 1.00
SBD 4.11