Skip to content
← Back to Community
Issues with math.h library in C ***FIXED***
Profile icon
18staleyn

Hello, I am relatively new to using C so I have absolutely no clue what in the world is going wrong with my code.

In my programming class, we are using C, and for a particular assignment, we were given a flow chart of logic and math statements as practice using if{} and else{} statements. The issue I am having, as stated with the title, is in relation to the math.h library, and in particular the log() function within the library.

Currently with how my program is set up, as you can check via the link at the bottom, I ask for an input for the variable 'x'. It then checks to see if x>10, if so, the variable 'y' is to be set as y=log(x). Seems like itd be fine and simple, right? Well apparently not. When I try to run my program, it spits this back out at me in the console:

exit status 1
/tmp/ccyKMGhF.o: In function `main':

main.c:(.text+0x5d): undefined reference to `log'
collect2: error: ld returned 1 exit status

So, to troubleshoot, i tried changing my variables from being floats to doubles. It didnt change. I tried setting them to integers (yes i know it wouldnt give me what i wanted if it did work, but i was just blindly throwing darts at a board with this one), didnt work. So I thought, "Ok, maybe theres a weird way to type log?", so i tried putting a value in that I knew what i should get the natural log of (roughly) and it ran just fine. No errors. Nothing wrong there.

At this point, I became extremely confused as to why it wasnt working because there is no other way to take the log() of a variable WITHOUT PUTTING THE VARIABLE IN THE LOG (or at least there isnt as far as i know). Please, someone who knows what the issue is, please help.

link:
https://repl.it/@18staleyn/FlowChartProgram

Edit: So, I will add that the assignment has been turned in as is and whatever I get, I get, but this issue may appear again later down the line for another program or another person may get this same error without knowing how to fix it. Thank you so much for the help if you know how to fix it.

Edit: Thank you for the help! I'll be sure to implement these fixes in future projects in case I run across this again. And hopefully, this helps others too.

Answered by abc3354 [earned 5 cycles]
View Answer
Voters
Profile icon
18staleyn
Comments
hotnewtop
Profile icon
abc3354

Hi !
Like @BigMitchyT and @Nanashi said, it is a bug with compiler flags.

However, there is a solution :
Right click on the editor > Command Palette > type Open shell > Press enter
In the shell which open, you can compile by hand
Basically, type gcc main.c -lm -o main then ./main to run your code.

Hope this will help ;)


Replit helper here !
If you found my answer helpful, you can upvote it :)

Profile icon
SamTheDev

Or you can edit the Makefile, and simply add that -lm flag in line 2
I installed Nano and copied the flag to the Makefile, and it worked!

Profile icon
BigMintyMitch

Based on my small knowledge of C/C++ and what I found on Google, you need to link the program to the Math library manually, because unlike <stdio.h> or <stdlib.h>, <math.h> does not automatically link to the program when it compiles.

However as far as I am concerned, there is no way to pass that command into the compiler on Repl.it. (If there is I'm flat out missing it)

Somebody else may know how to get it to work, but I do not see a way.

Profile icon
Nanashi

There's nothing wrong with your code but the compiler, which needs the -lm flag to use math.h. I see there are three bug reports mentioning math.h, all within the last week too. Hopefully the staff will do something about it.

The code works fine when copy & pasting to other sites.