C语言 如何将 Visual Studio Code 编译器/调试器设置为 GCC?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34057798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:14:56  来源:igfitidea点击:

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

cgccvisual-studio-code

提问by Thomas Woods

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 bit of digging I came across the Visual Studio Marketplace. This seemed like the right sort of thing, but only four uncommon languages were there.

我在 Visual Studio Code 中使用 C 进行编程,但我无法编译,因为 VSC 仅提供三个内置编译器 - Node.js、C# Mono 和扩展开发。经过一番挖掘,我发现了Visual Studio Marketplace。这似乎是正确的事情,但那里只有四种不常见的语言。

I can only assume that C debugging support is built in, I just can't find it or I am going the wrong way about doing it. I attempted to create a new launch.json (the manifest that seems to hold the compiling/debugging settings for each file) and manually entering the GCC binaries that I have, but that didn't end up working. I'm currently stuck manually compiling the C source file I am working on through command prompt.

我只能假设 C 调试支持是内置的,我只是找不到它,或者我做错了。我尝试创建一个新的 launch.json(似乎包含每个文件的编译/调试设置的清单)并手动输入我拥有的 GCC 二进制文件,但最终没有成功。我目前无法通过命令提示符手动编译我正在处理的 C 源文件。

Would really help if someone could point me in the right direction on what to do.

如果有人能指出我该怎么做的正确方向,那真的会有所帮助。

tl;dr - Help from anyone debugging C in Visual Studio Code

tl;dr - 在 Visual Studio Code 中调试 C 的任何人的帮助

Windows 8, if that matters

Windows 8,如果这很重要

Cheers!

干杯!

回答by guest23

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

Ctrl+P 并键入“ext install cpptools”,它将安装调试 c 和 c++ 所需的一切。

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

在VS代码中调试很完整,但是如果只需要编译运行:https: //code.visualstudio.com/docs/languages/cpp

Look in the debugging section and it will explain everything

查看调试部分,它会解释一切

回答by WY Hsu

Caution

警告

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

友情提示:以下教程适用于Linux用户,而非 Windows

Tutorial

教程

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

如果你想用 GDB 调试你的 C++ 代码

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

您可以从 Visual Studio Code 官方网站阅读这篇(调试您的代码)文章。

Step 1: Compilation

第一步:编译

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

您需要设置task.json编译您的 cpp 文件

or simply type in the following command in the command window

或者只需在命令窗口中键入以下命令

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

to generate a debuggable .exefile

生成可调试.exe文件

Step 2: Set up the launch.jsonfile

第 2 步:设置launch.json文件

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

要启用调试,您需要生成一个launch.json文件

follow the launch.json example or google others

按照launch.json示例或谷歌其他人

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

第 3 步:按 ( Ctrl+ F5) 开始编译

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

launch.json当您按下快捷方式 ( Ctrl+ F5)时,此文件将启动配置

Enjoy it!

好好享受!

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

附注。想要设置的tasks.json可以从vscode官方(-> TypeScript Hello World)阅读

回答by Jun Han

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

使用 GCC 编译和运行 C 代码有一种更简单的方法,无需配置:

  1. Install the Code Runner Extension
  2. Open your C code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1and then select/type Run Code, or right click the Text Editor and then click Run Codein context menu, the code will be compiled and run, and the output will be shown in the Output Window.
  1. 安装代码运行器扩展
  2. 在文本编辑器中打开你的 C 代码文件,然后使用快捷方式Ctrl+Alt+N,或按下F1然后选择/键入Run Code,或右键单击文本编辑器然后Run Code在上下文菜单中单击,代码将被编译并运行,输出将显示在输出窗口。

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

此外,您可以根据需要使用不同的 C 编译器更新 settings.json 中的配置,C 的默认配置如下:

"code-runner.executorMap": {
    "c": "gcc $fullFileName && ./a.out"
}

回答by Enea Jahollari

For Windows:

对于 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\binto 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.
  1. 安装 MinGW 或 Dev C++
  2. 开放环境变量
  3. 在系统变量中选择路径 -> 编辑 -> 新建
  4. 将此复制C:\Program Files (x86)\Dev-Cpp\MinGW64\bin到新窗口。(如果您安装了 MinGW,请复制其 /bin 路径)。
  5. 要检查您是否已成功添加它:打开 CMD -> 键入“gcc”,它应该返回: gcc: fatal error: no input files compilation terminated.
  6. 为 Visual Studio Code安装C/C++&& C/C++ Compile Run|| 代码运行器
  7. 如果你只安装了 C/C++ Compile Run 扩展,你可以使用 F6/F7 编译你的程序
  8. 如果您安装了第二个扩展,您可以使用顶部栏中的按钮编译您的程序。

Screenshot: Hello World compiled in VS Code

截图:用 VS Code 编译的 Hello World

回答by Pavel

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 -gflag means that the compiler will insert debugging information into your executable, so that you can run gdb on it.

只是想补充一点,如果你想调试东西,你应该在调试之前用调试信息进行编译,否则调试器将无法工作。因此,在 g++ 中,您需要执行g++ -g source.cpp. 该-g标志意味着编译器会将调试信息插入到您的可执行文件中,以便您可以在其上运行 gdb。

回答by JerryGoyal

You need to install C compiler, C/C++ extension, configure launch.json and tasks.json to be able to debug C code.

您需要安装 C 编译器、C/C++ 扩展、配置 launch.json 和 tasks.json 才能调试 C 代码。

This article would guide you how to do it: https://medium.com/@jerrygoyal/run-debug-intellisense-c-c-in-vscode-within-5-minutes-3ed956e059d6

本文将指导您如何操作:https: //medium.com/@jerrygoyal/run-debug-intellisense-cc-in-vscode-within-5-minutes-3ed956e059d6

回答by Jeff Alyanak

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.

编辑:截至 2016 年 3 月,Microsoft 为 Visual Studio Code 提供了 C/C++ 扩展,因此我最初给出的答案不再有效。

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.

Visual Studio Code 不太支持 C/C++。因此,它不>自然地支持 Visual Studio Code 应用程序中的 gcc 或 gdb。它最多能做的是语法突出显示,C 不支持 >intellisense 等高级功能。您仍然可以编译和调试在 VSC 中编写的代码,但您需要在程序本身之外执行此操作。