0% found this document useful (0 votes)
5 views

UsingMathLib

This tutorial provides step-by-step instructions for configuring Visual Studio Code (VSCode) to compile C programs that include the <math.h> library by using the -lm option. It details the creation and editing of two essential files, tasks.json and launch.json, to enable proper compilation and debugging. Following these configurations allows users to run and debug their C code with the math library effectively in VSCode.

Uploaded by

emaz.kt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

UsingMathLib

This tutorial provides step-by-step instructions for configuring Visual Studio Code (VSCode) to compile C programs that include the <math.h> library by using the -lm option. It details the creation and editing of two essential files, tasks.json and launch.json, to enable proper compilation and debugging. Following these configurations allows users to run and debug their C code with the math library effectively in VSCode.

Uploaded by

emaz.kt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Tutorial on configuring VSCode to compile with <math.

h>
When you want to compile a program the includes <math.h> you will need to add another option
to your command line, -lm. This is not hard when your are using the command line but it requires
several steps to configure your VSCode to use the -lm.

After you have installed the two packages below and opened a folder in which you are working,
you will need to configure VSCode by creating and editing two files.

These files will be located in the folder you opened. If you always open your CMPT125 folder which
contains all your specific folders for assignments and labs then you will not have to make a new
copy of the two files in each folder. To create and edit these two files complete the following steps.

1. Select tha file containing the code you are developing in VSCode.
2. To create the first file, tasks.json,
a. Select “Configure Tasks” from the “Terminal” menu.

a. press the run and debug button then select to first C/C++ option in the popup window
below
b. The following tasks.json file should be created. You need to make one change.
Add one line containing the following characters (including the quotes), "-lm", after
line13. Make sure you save the edited tasks.json file.

3. Now we need to make the second file, the launch.json file. To do this
a. press the run and debug button on the left of your VSCode
screen (circled in the image below) to bring up the window below.

b. Select “create and launch a json file to get the following window
c. Press the add configuration button to give the following popup box
d. Select C/C++: (gdb) Launch. This will give you a launch.gson file containing the
following text. You must make the changes shown in bold red type.

// Use IntelliSense to learn about possible attributes.


// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc build active file"
}
4. Now you are ready to debug code with the math library (or any other C code). Press the run
and debug button on the left hand side of your window (circled in yellow below) to give you
the following window, then run your code using either the green arrow or the debug icons
(both circled in red below.
Warning: Adding break points after you have pressed the green icon or started the
execution of your code using the debug icons will cause errors. Set your breakpoints before
you press a button circled in red.

5.
6. After you have started you code an additional window will pop up, this is the terminal for
your program. Your screen will look something like the image below when your code is
running.

You might also like