As mentioned here: Undefined reference to pow( ) in C, despite including math.h, I can build C files that use math.h functions in Linux Ubuntu only in the terminal, by putting -lm
at the end of gcc -o namefile namefile.c
. But I want to build and run a C code that uses math.h in VSCode specifically. How do I do that?

- 89
- 2
- 2
- 6
-
2Add it to your libraries. – S.S. Anne Mar 07 '20 at 16:16
-
1How exactly do you "build and run a C code in VSCode"? Do you use some extension to run the compiler for you? – HolyBlackCat Mar 07 '20 at 16:33
-
I build and run them by pressing F6 and I use the extensions "Code Runner", "C/C++ Compile Run" and "C/C++" for C. – O Tal do Juca Mar 07 '20 at 17:54
1 Answers
You can do the same in VS Code using a custom task configuration for compiling your .c file.
Let's say we have this test.c file with 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));
}
Step 1: Create a tasks.json file
This is for compiling/building your codes.
To auto-create this:
- Open the .c file
- Open the command palette
- Select C/C++: Build and Debug Active File (added by the C/C++ extension)
- Select your compiler (ex. mine is
gcc-7
)
That will auto-create a tasks.json file and attempt to compile your .c file, which we expect to fail because it is missing the -lm
flag. So, edit the contents of the tasks.json file:
{
"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"
]
}
]
}
Here I added the -lm
flag to the gcc
arguments and label
-ed it as "Compile test.c". Modify the paths to the .c and .out files appropriately to match your environment.
More info on the schema here: https://code.visualstudio.com/docs/editor/tasks#_custom-tasks.
Step 2: Create a launch.json file
This is for running your codes.
To auto-create this:
- Open the command palette
- Select Debug: Open launch.json
- Select C++ (GDB/LLDB)
Then edit it to run the expected .out file.
{
"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
}
]
},
]
}
Notice that the preLaunchTask
should specify the same task label in tasks.json. Again, modify the paths appropriately to match your environment, especially the path and filename to the .out file.
Step 3: Compile and Run It
Now, I don't use (or like) Code Runner.
I use the built-in debugger configuration of VS Code.
Click the debugger from the left-hand side and select "Run test.c" from the dropdown.
This should compile your .c file, run it, and print out any outputs to the Terminal panel.
By default, the focus goes to the run output. But if you also want to see the compilation/build logs, you can select the Task from the dropdown.

- 25,369
- 29
- 96
- 135