O T*_*uca 0 c visual-studio-code
正如此处提到的:尽管包含 math.h,但在 C 中对 pow( ) 的未定义引用,我可以仅在终端中构建在 Linux Ubuntu 中使用 math.h 函数的 C 文件,方法是-lm在gcc -o namefile namefile.c. 但我想构建并运行一个专门在 VSCode 中使用 math.h 的 C 代码。我怎么做?
您可以在 VS Code 中使用自定义任务配置执行相同的操作来编译 .c 文件。
假设我们有 test.c 文件和 math.h。
#include <math.h>
#include <stdio.h>
#define PI 3.14159265 //defines the value of PI
/* Calculate the volume of a sphere from a given radius */
double volumeFromRadius(double radius) {
return (4.0/3.0) * PI * pow(radius,3.0f);
}
int main(void) {
double radius = 5.1;
printf("The volume for radius=%.2f is %.2f", radius, volumeFromRadius(radius));
}
Run Code Online (Sandbox Code Playgroud)
这是为了编译/构建您的代码。
要自动创建此:
gcc-7)这将自动创建一个tasks.json文件并尝试编译您的.c文件,我们预计该文件会失败,因为它缺少标志-lm。因此,编辑tasks.json文件的内容:
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile test.c",
"type": "shell",
"command": "/usr/bin/gcc-7",
"args": [
"-g",
"-o",
"${workspaceFolder}/Q/test.out",
"${workspaceFolder}/Q/test.c",
"-lm"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
在这里,我将-lm标志添加到gcc参数中并将label其编辑为“Compile test.c”。适当修改 .c 和 .out 文件的路径以匹配您的环境。
有关架构的更多信息,请访问:https ://code.visualstudio.com/docs/editor/tasks#_custom-tasks 。
这是为了运行您的代码。
要自动创建此:
然后编辑它以运行预期的 .out 文件。
{
"version": "0.2.0",
"configurations": [
{
"name": "Run test.c",
"preLaunchTask": "Compile test.c",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/Q/test.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/Q",
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
]
}
Run Code Online (Sandbox Code Playgroud)
请注意,应在tasks.jsonpreLaunchTask中指定相同的任务标签。再次,适当修改路径以匹配您的环境,尤其是 .out 文件的路径和文件名。
现在,我不使用(或不喜欢)Code Runner。
我使用 VS Code 的内置调试器配置。
单击左侧的调试器,然后从下拉列表中选择“运行 test.c”。
这应该编译您的 .c 文件,运行它,并将所有输出打印到终端面板。
默认情况下,焦点转到运行输出。但如果您还想查看编译/构建日志,则可以从下拉列表中选择任务。
| 归档时间: |
|
| 查看次数: |
12261 次 |
| 最近记录: |