473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

logarithm of a variable in C

Hey, I have a problem with following code:


#include <stdio.h>
#include <math.h>

int main() {
int x;
x = 10;
printf("%.2f\n", log(x));
}

when I try to compile it in GCC it shows an error message:
In function `main':
logarithm.c:(.text+0x19): undefined reference to `log'

how can I fix it?

Thans James
Dec 14 '11 #1
11 2522
Banfa
9,065 Expert Mod 8TB
It compiles for me (using MinGW) which suggests a problem either with your compiler command line or with missing standard libraries or with the path to the libraries being wrong.
Dec 14 '11 #2
donbock
2,426 Expert 2GB
Are you using C or C++?
Change the definition of x so that it is a double.

C++ supports three different functions all named log. It uses magic to determine which of the three to use based on the type of the argument. If you're using C++ then perhaps passing an int argument fooled the magic into looking for a nonexistent fourth version of log.
Dec 14 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
C++ would automatically convert the int to a float so I suspect Banfa is correct and a library has been left out of the build.
Dec 14 '11 #4
I'm using C, probably there's problem with compiler, when I try to compile and run the program in codeblocks everything was good, no errors

Thanks alot for your responses
Dec 14 '11 #5
donbock
2,426 Expert 2GB
Try adding the -lm switch to the compiler command line. This tells it to link in library m, which is actually a file named libm.a This is where the math library is typically located.
Dec 14 '11 #6
It doesn't work :(, if it helps I've got Ubuntu 11
I don't know what's wrong
Dec 15 '11 #7
Banfa
9,065 Expert Mod 8TB
Have you installed the build-essential package ?

sudo apt-get install build-essential
Dec 15 '11 #8
I haven't, thank you, I'll try and see if it will help
Dec 15 '11 #9
it didn't help :(, but thank you very much for your responses
Dec 15 '11 #10
nubera
1
I don't know exactly why it helps, but it does: use an optimization flag (-O1..O3) when compiling:

Expand|Select|Wrap|Line Numbers
  1. gcc -O1 myprogram.c
Hope that helps in your case too. Cheers.
May 3 '13 #11
johny10151981
1,059 1GB
compile with -lm parmater
gcc -lm myprogram.c

somebody did the advice already :)
May 4 '13 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
10
by: David Casey | last post by:
I'm working on a program for my C++ class and have it all written and working except for one part. I need to compare two numeric variables to determine decimal accuracy between them. For example:...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
1
by: KamahlAlf | last post by:
I need to calculate the logarithm in base 2 of a number that is a power of 2 in c using bitwise operator . I have done it in this way: int tmp=1,log2n=0 for(;;){ if(n==tmp) break; ...
5
by: VamaVecheDan | last post by:
I can;t find anyware a java script that calculates logarithm and exponential!... It;s for my school project...I have 4 hours left to finish it... can anyone help me, please?!
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
1
by: rengaraj | last post by:
hello friends i am looking for algorithm of logarithm function.how does it work?could you help me on this or send me sample codes thanks in advance
7
by: wilson | last post by:
int main() { int main=100; printf("%d",main); } o/p 100 can we use the function name as a variable...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.