How to run a C program in Visual Studio Code

Asked

Viewed 65,838 times

9

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

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

Project structure:

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


My code:

- c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": ["/usr/include"],
            "browse" : {
                "limitSymbolsToIncludedHeaders" : true,
                "databaseFilename" : ""
            }
        },
        {
            "name": "Linux",
            "includePath": ["/usr/include"],
            "browse" : {
                "limitSymbolsToIncludedHeaders" : true,
                "databaseFilename" : ""
            }
        },
        {
            "name": "Win32",
            "includePath": ["c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include"],
            "browse" : {
                "limitSymbolsToIncludedHeaders" : true,
                "databaseFilename" : ""
            }
        }
    ]
}
- tasks.json
{
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "showOutput": "always",
    "args": ["-g", "main.c"]
}
- c program
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{  
    printf("Hello World");
    return 0;
}

Link to the Pastebin

7 answers

15


Vscode, though both an editor and not an IDE, allows you to compile code through a few plugins. In case of compiling C/C++ codes, in Windows, I found it a little "boring". It took me a week to set up.

Even though it was the question asked over a year ago, I believe some people might find it and I might have helped in some way.

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

Once installed the plugin, download and install the Minggw with gcc functions for Windows.

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

Remember that the version of the file 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 the C language, not C++, just remove a few 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 setting, just add "C_Cpp.intelliSenseEngine": "Default" in your user settings.

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

Source: Github from Microsoft

6

Boy suffered too much to run C in Visual Studio (Windows), I found out in trial and error and added a script to facilitate use. I did so:

  • Tighten CTRL+SHIFT+X in Visual Studio to Open the Extensions and install the C/C++ from Microsoft (to complete the codes) and Code Runner (to Execute).
  • Download the mingw compiler.
  • Install the Mingw (notes the directory where installed) and open the Mingw Installer, goes on Basic Setup and marks the mingw32-base-bin, Installation -> Apply Changes.
  • Go to environment variables on Windows -> System variables -> Path, and add the folder bin of the Mingw you installed.
  • Back in VS Code -> Right Button in Code Runner -> Configure Extension Settings. User Tab -> Extensions. Search Code-Runner: Executor Map and goes on Edit in Settings.json (The path here is in C: Users Username Appdata Roaming Code User Settings.json) and leave it at that:

    {
    "git.ignoreMissingGitWarning": true,
    "explorer.confirmDragAndDrop": false,
    "explorer.confirmDelete": false,
    "editor.fontSize": 16,
    "editor.accessibilitySupport": "on",
    "editor.autoClosingBrackets": "always",
    "editor.autoClosingQuotes": "always",
    "terminal.integrated.rendererType": "dom",
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
    "C_Cpp.updateChannel": "Insiders",
    "extensions.ignoreRecommendations": false,
    "code-runner.executorMap": {
    "javascript": "node",
    "php": "C:\\php\\php.exe",
    "python": "python",
    "perl": "perl",
    "ruby": "C:\\Ruby23-x64\\bin\\ruby.exe",
    "go": "go run",
    "html": "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"",
    "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
    "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && start $fileNameWithoutExt.exe"
    },
    "code-runner.clearPreviousOutput": true,
    "code-runner.runInTerminal": true,
    "code-runner.saveFileBeforeRun": true
    }
    

Just edit or add the "c" line and the last 3 lines, the rest is default.

  • Just write your code, save to extension . c and run it with CTRL+ALT+N that everything will be done on Windows CMD ;-].

PS1: If it doesn’t work, press CTRL+ALT+J in your code and select C.

PS: The anti-virus here (KIS) is blocking the execution all the time but just allow it to open normal.

6

Visual Studio Code is a text editor, not an IDE, without a compiler. You can compile from the terminal (whether you use Linux or Mac):

gcc -o executavel main.c
  • What does that flag mean -o, I’ve always been curious to know if I ever asked ;) ?

  • -o comes from -- output, i.e., output, the output file of that build, right after that flag must indicate the name of the executable that the build will generate.

  • 1

    visual studio code compiles even python friends. and according to this tutorial: https://code.visualstudio.com/docs/languages/cpp, c and c++ also, but I’m just not being able to configure my json correctly.

  • Actually it doesn’t have to be exactly mingw, with me Cygwin also worked, also works with cmake (although not to say if it is possible in the plugin page), I had to create two tasks, one for the cmake build and another pro make, Microsoft could simplify the VS Code environment preparation process, such as a Project Manager, for example.

2

You don’t need all these settings. Just install Microsoft C module. CTRL+SHIFT+´ to open the terminal. It already opens in the file folder.

The commands to execute are:

$ g++ testeC.cpp -o testeC
$ ./testeC

Where testeC.cpp is the name of your file with extension (in C would: testeC. c)

PS.:

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

  • The best would be the visual studio because of MSVC and its debugging tools, I found it strange that they make a version for the mac that is their biggest competitor for personal computers and not for linux leaving only a set of tools to compile linux programs on Windows, but personally code Blocks, Falcon or devc++ does not make much difference since they are only an editor, for me the most important is the compiler that will make the most important task, the similarity between them is that they use GNU compilers.

2

Complementing the friend answer above, you can use this code to run multiple files, just paste it into the C file: Users %username% Appdata Roaming Code User Settings.json, but first you need to install the Code Runner extension:

{ 
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
"code-runner.executorMap": {
    "c": "cls && gcc -g -o $fileNameWithoutExt *.c && $fileNameWithoutExt"
},
"code-runner.clearPreviousOutput": true,
"code-runner.runInTerminal": true,
}

0

after installing the microsoft c/c++ Intellisense plugin after installing the Runner plugin from jun han

have to put . exe after these words in the file Settings.json both in "c": and "cpp": the file location is the following address:

C:\Users\seu_nome_de_usuario\AppData\Roaming\Code\User\settings.json

$fileNameWithoutExt
$dir$fileNameWithoutExt

changing to

$fileNameWithoutExt.exe
$dir$fileNameWithoutExt.exe

this is a solution for windows because executables compiled for linux have no extension

0

Hi, this is a problem I faced when I first started using VS. If you like me, I would just like to see the output in the terminal, and not waste a lot of time configuring, and do only once quickly and simply. I used the Mingw with the extension of VS, C/C++ Configurations.

First place the Mingw bin directory as the Windows environment variable next to the PATH

Then in VS, open C/C++ Configurations, (Ctrl+Shift+P ">C/C++:Edit Configurations (UI)") put the following settings:

Compiler path:

C:\MinGW\bin\g++.exe
                    Caso não seja este o local onde está o MinGw

Compiler Arguments:

-g
-Wall
-o
${fileBasenameNoExtension}.exe
${filename}

Modo do Intellisense

gcc-x64

The rest keep, as is or upgrade to the latest, for C or C compiler++

And then it will start generating the following file c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\MinGW\\bin\\g++.exe",
            "cStandard": "c11",
            "cppStandard": "c++20",
            "intelliSenseMode": "gcc-x64",
            "compilerArgs": [
                "-g",
                "-Wall",
                "-o",
                "${fileBasenameNoExtension}.exe",
                "${filename}"
            ]
        }
    ],
    "version": 4
}

Ctrl+Alt+N that will start compiling and appear in the terminal. Otherwise...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.