0

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?

O Tal do Juca
  • 89
  • 2
  • 2
  • 6

1 Answers1

6

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:

  1. Open the .c file
  2. Open the command palette
  3. Select C/C++: Build and Debug Active File (added by the C/C++ extension)
  4. 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:

  1. Open the command palette
  2. Select Debug: Open launch.json
  3. 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.

enter image description here

This should compile your .c file, run it, and print out any outputs to the Terminal panel.

enter image description here

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135