<math.h> math module is not working for me in C, i am using VSCodium in linux

69 views Asked by At

so my code ran smoothly in codium with the extensions when i was only working with stdio.h, but when i tried to use math.h module in my program i am unable to run it, this is what i get as error:

Executing task: /bin/bash -c gcc -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wcast-align -Wconversion -Wsign-conversion -Wnull-dereference -g3 -O0 -c /home/suvansh/Documents/Cstuff/new.c -o ./build/Debug/new.o && gcc -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wcast-align -Wconversion -Wsign-conversion -Wnull-dereference -g3 -O0 ./build/Debug/new.o -o ./build/Debug/outDebug

/usr/lib64/gcc/x86_64-suse-linux/13/../../../../x86_64-suse-linux/bin/ld: ./build/Debug/new.o: in function main': /home/suvansh/Documents/Cstuff/new.c:97:(.text+0xb5): undefined reference to pow' collect2: error: ld returned 1 exit status

  • The terminal process "/bin/bash '-c', 'gcc -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wcast-align -Wconversion -Wsign-conversion -Wnull-dereference -g3 -O0 -c /home/suvansh/Documents/Cstuff/new.c -o ./build/Debug/new.o && gcc -Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wcast-align -Wconversion -Wsign-conversion -Wnull-dereference -g3 -O0 ./build/Debug/new.o -o ./build/Debug/outDebug'" failed to launch (exit code: 1).
  • Terminal will be reused by tasks, press any key to close it.

when i comment the line which uses the math module, my code works fine

i tried running gcc in terminal (i have never ran or compiled C in terminal i have always used codium or online compiler i tried this after some googling) so i tried "gcc -c new.c -lm -o new.o" (new.c is my file name) i was expecting for the code to run but nothing happened, after i typed it it just went to the next line but a new, new.o file was made.

i need a solution on how to actually fix this, is it a problem inside vscodium or am i missing something to install in my system ?

1

There are 1 answers

0
Suvansh Rao On

Ok so i got it fixed with help from the comments, i wasnt aware you have to run "./new" to execute it and that i have to include "-lm" after "gcc -o new new.c". I read through the documentation of the extension and figured out how to add extra arguments to the gcc compiler so now it works in the IDE as well and i know to run it from the terminal too. apparently you have to edit the settings.json in the .vscode directory which is created locally, i had to add

"C_Cpp_Runner.compilerArgs":[
    "-lm"
],

i had to add "-lm" in the settings.json and when i tried running it after, it worked properly.

note that i am not using the C/C++ extension from microsoft, i am using https://marketplace.visualstudio.com/items?itemName=franneck94.c-cpp-runner so this fix only works for this.