Back to General discussions forum
Context: I've been trying to learn C with this site for quite some time now, and so far, it's been going pretty good. I prefer to stick exlusively to C if I can help it, as I heard from quite a few places that low-level languages like C can really do you a lot of favours when it comes to programs that work on desktops. Or laptops. Computers, really. Also the (dot) instead of a period is because post titles don't let you use those for some reason.
Main Problem and Potential Solution?: Whenever I try using math.h in any code I write, the compiler claims that the functions I'm using (e.g sqrt(), abs(), pow()) are undefined references to said functions. Not even sure they work properly if the code does properly compile. Normally this wouldn't be a big issue, but there's some tasks out there (e.g Primes, Triange Area, anything that needs complex formulas) that math.h would really help out in, if not an outright requirement.
Looking this problem up on Google, I found this Arch forum link, and this StackOverflow page, it might be a case of an improperly set-up GCC? Or maybe it's math.h that's wrong. Either way, a solution would be nice.
Hi Friend!
It's a known "feature", you just need to enable math library. math.h
is just the "header file" for that library, it
contains definitions (signatures of the functions) so that compiler knows how your code should call them. But adding
precompiled functions bodies to your program - that is done by linker. Normally it is called automatically just after
compiling - so as these references mention just -lm
option should be passed to the compiler, e.g.
instead of
$ gcc -o myprogram mysource.c
it should be like
$ gcc -o myprogram -lm mysource.c
There are few other sets of functions in the standard library which also require explicitly enabling parts of standard library.
Now where do you experience such issue? I guess it is not about the compiler in the "sandbox" on this site... If I enter some test code here and click "Run" button it works (and you may use this too for most problems).
If it is about your local setup, some more hints may be useful about how you installed or use GCC
. If you compile
your code simply in the command line (quite straightforward on Linux or OsX machines) - you just add the mentioned
option.
If you use some IDE (like CLion) with C inside, you need to find this in the settings of this IDE.
as I heard from quite a few places that low-level languages like C can really do you a lot of favours when it comes to programs that work on desktops.
well, C
nowadays is more often met in building firmware for network compomponents and electronic devices.
Decktop applications nowadays are not the most popular branch (you see, much software is written for mobile or web
platforms) and normally there are other choices of language/technology for them. However I readilly approve your
idea of learning pure C
as it definitely allows you to get better idea how the computer works (better, compared
to more high-level languages, but not comparable to assembly languages of course). It is very profitable for building
up debugging skills too :)
Ack, I must apologize for the late reply, I really wish I could link my email to this website so that I can get notifications. I'm not super-active on this website, it's usually only in short bursts.
However I'm actually experiencing this issue on the website's sandbox/IDE, but mostly with the C (Not ++) button. I think the arguments GCC gets with said button are different than if I were to simply press Run with C/C++ selected on the dropdown. Code runs fine on the latter, but bugs out on the former. No idea why.
[ it's me, Rodion again, just under test account ]
Wow, I'm so silly :) thanks a lot for explanation - completely forgot about separate button for "plain C". I found
both commands for g++
and gcc
were missing -lm
option from the beginning but somehow this doesn't affect ++
version.
Should be fixed now!
I really wish I could link my email to this website so that I can get notifications.
Well, thanks for the feedback - I thought about this few time but either was bit lazy or considered this could be annoying for people. Perhaps I should think a bit more in this direction :)
...additionally updated python version in the runner to 3.10.x
...
Thanks for the python update!