How to run a C program in Visual Studio Code

2

I'm trying to run a code (program) in C in Visual Studio Code, but I can not find the necessary settings.

  • I installed the C / C ++ (Microsoft) extension

Project structure:

.vscode          - c_cpp_properties.json          - tasks.json      main.c


My code: link

    
asked by anonymous 25.10.2016 / 14:47

3 answers

2

VSCode, although an editor and not an IDE, allows you to compile code through some plugins. In the case of compiling C / C ++ code in Windows, I find it a bit "boring". It took me a week to set up.

Even though the question was asked more than a year ago, I think some people might find this and I may have helped in some way.

What I did to compile the C code was to install the Microsoft C / C ++ plugin

After installing the plugin, download and install MingGW with the gcc functions for Windows.

In VSCode, create or edit the c_cpp_properties.json file, to edit just use the shortcut Ctrl + Shift + P and select "C / Cpp: Edit Configurations".

Remember that the file version quoted below may change.

{
    "configurations": [
        {
            "name": "Win32",
            "intelliSenseMode": "clang-x64",
            "includePath": [
                "${workspaceRoot}",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/mingw32",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include/c++/backward",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include",
                "C:/MinGW/include",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include-fixed"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "browse": {
                "path": [
                    "C:/MinGW/lib/gcc/mingw32/6.3.0/include",
                    "C:/MinGW/lib/gcc/mingw32/6.3.0/include-fixed",
                    "C:/MinGW/include/*"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ]
}

For C language, not C ++, just remove some lines, as below:

{
    "configurations": [
        {
            "name": "Win32",
            "intelliSenseMode": "clang-x64",
            "includePath": [
                "${workspaceRoot}",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include",
                "C:/MinGW/include",
                "C:/MinGW/lib/gcc/mingw32/6.3.0/include-fixed"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "browse": {
                "path": [
                    "C:/MinGW/lib/gcc/mingw32/6.3.0/include",
                    "C:/MinGW/lib/gcc/mingw32/6.3.0/include-fixed",
                    "C:/MinGW/include/*"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ]
}

After performing this configuration, you only add "C_Cpp.intelliSenseEngine": "Default" to your user settings.

Then just write the code and run it with the Code Runner plugin.

Source: Microsoft GitHub

    
07.03.2018 / 23:02
2

Visual Studio Code is a text editor, not an IDE, it has no compiler. You can compile through the terminal (if you use Linux or Mac):

gcc -o executavel main.c
    
25.10.2016 / 15:14
0

Man, I need these settings, not everything. Just install Microsoft's C module. ctl + shift + 'to open the terminal. It already opens in the file folder.

The commands to execute are:

$ g ++ testC.cpp -o testC

$ ./testeC Where testC.cpp is the name of your static with extension (in C would be: testC.c)

PS.:

My personal opinion about programming in C, C ++ is best to install code :: blocks (Windows, Linux or Mac).

    
02.06.2018 / 23:06