How does one set up the Visual Studio Code compiler/debugger to GCC?

前端 未结 7 512
野趣味
野趣味 2020-12-13 17:56

I am programming in C in Visual Studio Code, but I can\'t compile, as VSC only offers three compilers built in - Node.js, C# Mono, and Extension development. After a little

相关标签:
7条回答
  • 2020-12-13 18:17

    Caution

    A friendly reminder: The following tutorial is for Linux user instead of Windows

    Tutorial

    If you want to debug your c++ code with GDB

    You can read this ( Debugging your code ) article from Visual Studio Code official website.

    Step 1: Compilation

    You need to set up task.json for compilation of your cpp file

    or simply type in the following command in the command window

    g++ -g file.cpp -o file.exe
    

    to generate a debuggable .exe file

    Step 2: Set up the launch.json file

    To enable debugging, you will need to generate a launch.json file

    follow the launch.json example or google others

    Step 3: Press (Ctrl+F5) to start compiling

    this launch.json file will launch the configuration when you press the shortcut (Ctrl+F5)

    Enjoy it!

    ps. For those who want to set up tasks.json, you can read this from vscode official (-> TypeScript Hello World)

    0 讨论(0)
  • 2020-12-13 18:22

    For Windows:

    1. Install MinGW or Dev C++
    2. Open Environment Variables
    3. In System Variable select Path -> Edit -> New
    4. Copy this C:\Program Files (x86)\Dev-Cpp\MinGW64\bin to the New window. (If you have MinGW installed copy its /bin path).
    5. To check if you have added it successfully: Open CMD -> Type "gcc" and it should return: gcc: fatal error: no input files compilation terminated.
    6. Install C/C++ for Visual Studio Code && C/C++ Compile Run || Code Runner
    7. If you installed only C/C++ Compile Run extension you can compile your program using F6/F7
    8. If you installed the second extension you can compile your program using the button in the top bar.

    Screenshot: Hello World compiled in VS Code

    0 讨论(0)
  • 2020-12-13 18:27

    Just wanted to add that if you want to debug stuff, you should compile with debug information before you debug, otherwise the debugger won't work. So, in g++ you need to do g++ -g source.cpp. The -g flag means that the compiler will insert debugging information into your executable, so that you can run gdb on it.

    0 讨论(0)
  • 2020-12-13 18:29

    EDIT: As of ~March 2016, Microsoft offers a C/C++ extension for Visual Studio Code and therefor the answer I originally gave is no longer valid.

    Visual Studio Code doesn't support C/C++ very well. As such it doesn't >naturally support gcc or gdb within the Visual Studio Code application. The most it will do is syntax highlighting, the advanced features like >intellisense aren't supported with C. You can still compile and debug code >that you wrote in VSC, but you'll need to do that outside the program itself.

    0 讨论(0)
  • 2020-12-13 18:30

    Ctrl+P and Type "ext install cpptools" it will install everything you need to debug c and c++.

    Debugging in VS code is very complete, but if you just need to compile and run: https://code.visualstudio.com/docs/languages/cpp

    Look in the debugging section and it will explain everything

    0 讨论(0)
  • 2020-12-13 18:34

    There is a much easier way to compile and run C code using GCC, no configuration needed:

    1. Install the Code Runner Extension
    2. Open your C code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in context menu, the code will be compiled and run, and the output will be shown in the Output Window.

    Moreover you could update the config in settings.json using different C compilers as you want, the default config for C is as below:

    "code-runner.executorMap": {
        "c": "gcc $fullFileName && ./a.out"
    }
    
    0 讨论(0)
提交回复
热议问题