Visual Studio Code Compile C (or C++)

In summary, Visual Studio Code (VSC) is a versatile tool that can be used for writing and executing code in various languages, including JavaScript, C, and C++. To run C or C++ code in VSC, you do not need to change your path environment variable. When you compile your code, VSC typically creates a directory structure with a solution file, the source code file, and an executable. To run the code in a command prompt, you can navigate to the directory with the executable and run it from there. To compile and link from a console window, you can use the VSC command prompt. Additionally, extensions are available for different languages, but they are not necessary for using VSC. To create a new project
  • #1
LtIvan
40
6
Allo,
I have got VSC and have been using it for JavaScript in Unity. Also, I am new to VSC. However, I have been wanting to use it for writing C and C++ for just standard compiling and executing. Is there some way to run '.c' Visual Studio Code in, let's say, on command prompt. I have heard I have to change my path in %PATH%.

Please provide some exact instructions.

Thanks in advance.
 
  • Like
Likes Pepper Mint
Technology news on Phys.org
  • #2
LtIvan said:
Allo,
I have got VSC and have been using it for JavaScript in Unity. Also, I am new to VSC. However, I have been wanting to use it for writing C and C++ for just standard compiling and executing. Is there some way to run '.c' Visual Studio Code in, let's say, on command prompt. I have heard I have to change my path in %PATH%.

Please provide some exact instructions.

Thanks in advance.
You don't need to change your path environment variable. When you compile your C or C++ code, Visual Studio typically creates a directory with the same name as your project, and it also creates two directories under that one. One of the inner directories has the same name as the outer project directory, and the other one is called Debug.
If your project is named, say ReadStruct, the directory structure that VS creates by default is this:
ReadStruct - contains the solution file with a .sln file extension
ReadStruct - contains your C source code file
Debug - contains the executable, with a .exe extension

To run your code in a command prompt, open a command prompt window using is cmd.exe. You can start this by Start --> Run, and type cmd.exe.
In the command prompt window, change directory (CD) to the directory with the executable. When that directory is the current directory, type the name of the executable file, and you program should start running.
 
  • #3
Visual Studio also includes the ability to compile and link from a console window. Click on start / programs / Visual Studio ... / ... / Command Prompt to open up a console window with the environment variables setup to do compiles and links.
 
  • #4
Mark44 said:
You don't need to change your path environment variable. When you compile your C or C++ code, Visual Studio typically creates a directory with the same name as your project, and it also creates two directories under that one. One of the inner directories has the same name as the outer project directory, and the other one is called Debug.
If your project is named, say ReadStruct, the directory structure that VS creates by default is this:
ReadStruct - contains the solution file with a .sln file extension
ReadStruct - contains your C source code file
Debug - contains the executable, with a .exe extension
I don't think I am even up to compiling. Sorry but I do not yet know how to compile.
Also, where do I find these?
ReadStruct - contains the solution file with a .sln file extension
ReadStruct - contains your C source code file
Debug - contains the executable, with a .exe extension
 
  • #5
Is just that I do not know how to compile yet in VSC.
Could someone show me how?
 
  • #6
LtIvan said:
I don't think I am even up to compiling. Sorry but I do not yet know how to compile.
Also, where do I find these?
You don't find them. These are the directories that are produced from a program I wrote.
LtIvan said:
ReadStruct - contains the solution file with a .sln file extension
ReadStruct - contains your C source code file
Debug - contains the executable, with a .exe extension

Do you know how to start Visual Studio?
 
  • #7
Mark44 said:
You don't find them. These are the directories that are produced from a program I wrote.Do you know how to start Visual Studio?
I kinder of new to VSC. What I do is 'save' which I think is something different to what you are talking about which I do not know. Yes I can open up VSC and type something on it.
I just need to know how to compile it and know where are the directories.
 
  • #8
When you start Visual Studio, look at the menu bar at the top. The menu choice all the way to the right is HELP. Click that one, which will open up your browser to a page on MSDN (Microsoft Developer Network). In the pane that says "Get started" click Visual C++. That should get you started.
 
  • #10
LtIvan said:
I figured I might need the C/C++ extension installed.
https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
However, when I tried to install it by typing 'ext install cpptools' in the command line (Ctrl+P), it comes up with this message:
"unable to get local issuer certificate".
I don't know why you would need an extension. Visual Studio should work with any of the languages that Microsoft supports: C/C++, C#, Visual Basic, F#, Python, JavaScript, ...

What version of VS do you have?
 
  • #11
0.10.11
I think it's just that I am having trouble understanding 'projects' and 'folders'. For example, how do I make a new project in C or open one?
Just try giving the exact steps I should do say if I have just gotten VSC on my PC.
That might help understand things.
 
  • #12
LtIvan said:
0.10.11
I don't recognize this as a version number. On the menu bar, under HELP, the last menu item is About Microsoft Visual Studio. On my installation, selecting this menu choice brings up this window:
VS.png


I have Visual Studio 2013, which is version 12.0 + some other stuff.
LtIvan said:
I think it's just that I am having trouble understanding 'projects' and 'folders'. For example, how do I make a new project in C or open one?
A folder is a directory. A project consists of the C code that you write together with all of the files that Visual Studio creates when it compiles and links your code to produce an executable (the "program").
When VS starts, it shows you this page (the Start Page). To create a new project, click New Project.

If you have already created a project and want to open it, click Open Project... Under Recent it will show the projects you have recently worked on, if any.
VSStart.jpg

LtIvan said:
Just try giving the exact steps I should do say if I have just gotten VSC on my PC.
That might help understand things.
Did you look at the things I suggested under HELP in my previous post? If you have specific questions about any of that material ask, but I don't feel like reproducing what's already there.
 
Last edited:
  • #13
1.1.0

I think I have found the initial root of this problem.
I literally don't have that 'Startup page' when Visual Studio Code starts.
It just opens up on the most recent file or just a new blank file.
I think this may be the issue?

However, sorry for this long dilemma.
You have given enough information. I may have to take this further.
Thanks in advance.
 
  • #14
Hey LtIvan,

If you want to compile these sorts of files you should start a basic Win32 style C++ project and add the files manually.

Just note that Visual Studio looks for certain entry functions (namely WinMain entry point).

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633559(v=vs.85).aspx

You will have to also be aware that VS imports stuff for you (that you may not declare) and you will have to be aware that if you use standard libraries then you will need to use the headers provided with VS and not external ones.

The C/C++ style is a little "old" (in terms of software which changes so rapidly) so you should look at the basic C++ Win32 template.

If you use MFC then that is different along with COM (which is even more different) so I'd recommend sticking with the Win32 style C++ project and then adding your files and the WinMain entry point.
 
  • #15
chiro said:
You will have to also be aware that VS imports stuff for you (that you may not declare) and you will have to be aware that if you use standard libraries then you will need to use the headers provided with VS and not external ones.

The C/C++ style is a little "old" (in terms of software which changes so rapidly) so you should look at the basic C++ Win32 template.
Since the OP has stated that he's interested in writing C code that runs in the console, I would recommend that he start with a C++ Empty Project, rather than a Win32 Console Application. At the early stages of learning to write C code, with input coming from the keyboard and output going to the screen, the only header that is needed is stdio.h. This header can be included with the following line of code:
C:
#include <stdio.h>
The template that is used for Win32 console apps can seem a bit mysterious to someone new to programming, looking like this:
C:
#include"stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
   return 0;
}
First, having _tmain in the code instead of main probably won't agree with whatever the OP is using to follow, and second, the arguments to main(), especially the array argument (of type _TCHAR *) will seem very mysterious.
chiro said:
If you use MFC then that is different along with COM (which is even more different) so I'd recommend sticking with the Win32 style C++ project and then adding your files and the WinMain entry point.
It's highly unlikely, IMO, that the OP is going to be using MFC in his program.
 
  • #16
Yes I was hoping to just start with a simple code:
Code:
#include <stdio.h>
int main()
{
    printf( "I am alive!  Beware.\n" );
    getchar();
    return 0;
}

However, the actual problem is that I can't seem to get the startup page with the 'New Project' which is what I need.
Is it something to do with my version?
When I start VSC, it comes up with this, (fig.1).
Is there something wrong?
 

Attachments

  • vsc.png
    vsc.png
    9.3 KB · Views: 918
  • #17
I still don't know what version you have. In post #11 you said it was version 0.10.11, which doesn't make sense. In post #13, you said 1.1.0, which also doesn't make sense. From the image you show in post #16, I can't tell much of anything. If that's what shows when you start up VS, maybe you don't have the complete installation of this software. Can you get any information from the HELP menu, under About Microsoft Visual Studio? (My suggestion in post #12.)
 
  • #18
I think we all are confused. I think your talking about standard "Visual Studio", I talking about "Visual Studio Code".
Anyway, I decided to get standard VS after figuring this out. I have created a new project and started a module but I am having problems. I do not think it's setup to 'C'. Everytime I keep compiling it, it comes up with a load of errors. Do I have specify the language and what now from here?
 
  • #19
I get it now. I've been using Visual Studio for many years, but "Visual Studio Code" is a new one on me.

When you create a New Project, VS opens a window that shows a number of templates. Select Visual C++. In the pane at the right, I usually select Empty Project.

If you expand Other Languages, you'll see Visual Basic and Visual C# and a few others. You don't want any of those -- the template should be for C++.
At the bottom of the pane, type the name of your project in the Name field. VS will call your first project Project1, by default, but it's better to make up your own name for the project.
VSnew.jpg

What errors are you getting? Often, the first error listed is the one that is causing the problem. Fixing it elimanates the following errors. Note that VS distinguishes between errors and warnings, with errors being more severe. Copy the information in the first few errors and we can probably figure out what's going wrong. Be sure to include the full text of the error/warning, which includes the line number where the error occurred and some text about what the error is.
 
  • #20
I only get C# and Basic, not C under templates. Do I have to install a C++ or C template? If so where would I find it.
I have found a bunch of this ones online, (fig.1). Are they any good? I have tried some but do not work.
 

Attachments

  • ccc.png
    ccc.png
    68.1 KB · Views: 1,030
  • #21
Use the C++ template, which is what you show in the image you post. You can write C code using the C++ template.
 
  • Like
Likes LtIvan
  • #22
I was able to get a C++ template. However, when I tried building that simple C file, it came up with an error saying I haven't got Windows SDK 8.1 installed. So I installed Windows SDK 8.1 and that fixed it but I have gotten another error when I try to build saying: "
Error TRK0005 Failed to locate: "CL.exe". The system cannot find the file specified.
 
  • #23
cl.exe is the compiler itself.
If you installed Visual studio to the default directory then your file search paths and environment variables should automatically be correct.
If you chose some other directory then these may need to be manually reconfigured
 
  • #24
While I try a figure this issue out, I have decided to use Code::Blocks, which is a very simple C/C++ compiler and I might stick with it.
Thanks all with the support and I will try to solve this.
 
  • #25
Over the years, I have had a few versions of Visual Studio Express (2008, 2010, currently with 2015), and haven't had any issues compiling C or C++ programs, but almost all of my programs are console programs. Versions 2010 and older can be run with Win XP. I'm running VS 2015 with Win 7. For java, I'm using netbeans 8.1, along with whatever version of Java it comes with.
 
  • #26
I agree with what rcgldr said. I have used Visual Studio in many different versions, without any problems. If the system can't find cl.exe, it's likely a problem with how it was installed.
 

1. How do I set up Visual Studio Code to compile C or C++ code?

To set up Visual Studio Code to compile C or C++ code, you will need to install the C/C++ extension. This can be done by going to the extensions tab in the sidebar and searching for "C/C++". Once the extension is installed, you will need to set up a tasks.json file to specify the compiler and build options for your code.

2. How do I run my compiled C or C++ code in Visual Studio Code?

To run your compiled C or C++ code in Visual Studio Code, you will need to create a launch.json file. This file will specify the executable file to run and any necessary command line arguments. You can then use the "Run" button in the debug panel to execute your code.

3. Can I use Visual Studio Code to debug my C or C++ code?

Yes, Visual Studio Code has a built-in debugger that can be used for C and C++ code. To use the debugger, you will need to create a launch.json file and set the "type" to "cppdbg". You can then use the debug panel to set breakpoints and step through your code.

4. How do I add additional libraries or dependencies to my C or C++ project in Visual Studio Code?

To add additional libraries or dependencies to your C or C++ project in Visual Studio Code, you will need to modify your tasks.json file. You can add any necessary compiler flags or include paths to the "args" section of the file. You can also use the "dependsOn" property to ensure that your libraries are built before your main code is compiled.

5. Can I use Visual Studio Code to build and compile my C or C++ code on multiple platforms?

Yes, Visual Studio Code can be used to build and compile C or C++ code on multiple platforms. You will need to install the necessary compilers and tools for each platform, and then configure your tasks.json and launch.json files accordingly. You can also use the "os" property in your tasks.json file to specify different build commands for different operating systems.

Similar threads

  • Programming and Computer Science
Replies
0
Views
238
  • Programming and Computer Science
Replies
1
Views
262
  • Programming and Computer Science
Replies
2
Views
310
  • Programming and Computer Science
Replies
3
Views
680
Replies
6
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
1
Views
687
  • Programming and Computer Science
Replies
22
Views
2K
Back
Top