Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Visual Studio Code Compile C (or C++)

  1. May 5, 2016 #1
    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.
     
  2. jcsd
  3. May 6, 2016 #2

    Mark44

    Staff: Mentor

    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.
     
  4. May 6, 2016 #3

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  5. May 6, 2016 #4
    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
     
  6. May 9, 2016 #5
    Is just that I do not know how to compile yet in VSC.
    Could someone show me how?
     
  7. May 9, 2016 #6

    Mark44

    Staff: Mentor

    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?
     
  8. May 9, 2016 #7
    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.
     
  9. May 9, 2016 #8

    Mark44

    Staff: Mentor

    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. May 9, 2016 #9
  11. May 9, 2016 #10

    Mark44

    Staff: Mentor

    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?
     
  12. May 10, 2016 #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.
     
  13. May 10, 2016 #12

    Mark44

    Staff: Mentor

    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.
    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
    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: May 10, 2016
  14. May 12, 2016 #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.
     
  15. May 13, 2016 #14

    chiro

    User Avatar
    Science Advisor

    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.
     
  16. May 13, 2016 #15

    Mark44

    Staff: Mentor

    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:
    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:
    Code (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.
    It's highly unlikely, IMO, that the OP is going to be using MFC in his program.
     
  17. May 13, 2016 #16
    Yes I was hoping to just start with a simple code:
    Code (C):
    #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?
     

    Attached Files:

    • vsc.png
      vsc.png
      File size:
      15.3 KB
      Views:
      241
  18. May 13, 2016 #17

    Mark44

    Staff: Mentor

    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.)
     
  19. May 14, 2016 #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?
     
  20. May 14, 2016 #19

    Mark44

    Staff: Mentor

    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.
     
  21. May 14, 2016 #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.
     

    Attached Files:

    • ccc.png
      ccc.png
      File size:
      143.8 KB
      Views:
      280
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Visual Studio Code Compile C (or C++)
Loading...