Visual Studio Code Compile C (or C++)

  • C/++/#
  • Thread starter LtIvan
  • Start date
  • #1
40
6

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
33,270
4,966
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
rcgldr
Homework Helper
8,680
514
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
40
6
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
40
6
Is just that I do not know how to compile yet in VSC.
Could someone show me how?
 
  • #6
33,270
4,966
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
40
6
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
33,270
4,966
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.
 
  • #9
40
6
  • #10
33,270
4,966
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
40
6
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
33,270
4,966
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
40
6
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
chiro
Science Advisor
4,790
131
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
33,270
4,966
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
40
6
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

  • #17
33,270
4,966
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
40
6
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
33,270
4,966
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
40
6
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

  • #21
33,270
4,966
Use the C++ template, which is what you show in the image you post. You can write C code using the C++ template.
 
  • #22
40
6
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
3,379
942
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
40
6
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
rcgldr
Homework Helper
8,680
514
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.
 

Related Threads on Visual Studio Code Compile C (or C++)

  • Last Post
Replies
7
Views
25K
Replies
17
Views
810
Replies
10
Views
2K
  • Last Post
Replies
6
Views
3K
Replies
6
Views
9K
Replies
3
Views
3K
Replies
7
Views
2K
Replies
6
Views
4K
Replies
5
Views
833
Replies
5
Views
6K
Top