public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* math.h prob
@ 1999-11-03  8:21 Dave Brennan
  1999-11-03  8:27 ` Art Haas
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: Dave Brennan @ 1999-11-03  8:21 UTC (permalink / raw)
  To: help-gcc

Hi,

I am fairly new to C programming, especially to using gcc, so I am sorry
if my question is a bit simple!

I am using gcc 2.8.1 on a Sun workstation (solaris 2.7).

I am having problems using maths functions. e.g if I have the program

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

main()
{

double x;

scanf("%lf",&x);

printf("x = %lf",sqrt(x));


}

and try to compile it using,

gcc -o test mattest.c

I get the following error:

Undefined                       first referenced
 symbol                             in file
sqrt                                /var/tmp/cc9Eay_r1.o
ld: fatal: Symbol referencing errors. No output written to test

Does anyone know why this is happening?

Thanks for the help.


Dave Brennan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: math.h prob
  1999-11-03  8:21 math.h prob Dave Brennan
@ 1999-11-03  8:27 ` Art Haas
  1999-11-30 23:28   ` Art Haas
  1999-11-03  8:28 ` haha
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Art Haas @ 1999-11-03  8:27 UTC (permalink / raw)
  To: help-gcc

Dave Brennan <9147261b@clinmed.gla.ac.uk> writes:

> Hi,
> 
> I am having problems using maths functions. e.g if I have the program
> 
> #include <stdio.h>
> #include <math.h>
> 
> main()
> {
> double x;
> scanf("%lf",&x);
> printf("x = %lf",sqrt(x));
> }
> 
> and try to compile it using,
> 
> gcc -o test mattest.c
> 
> I get the following error:
> 
> Undefined                       first referenced
>  symbol                             in file
> sqrt                                /var/tmp/cc9Eay_r1.o
> ld: fatal: Symbol referencing errors. No output written to test
> 

You need to link in the math library ...

% gcc -o test mattest.c -lm

-- 
###############################
# Art Haas
# (713) 689-2417
###############################

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: math.h prob
  1999-11-03  8:21 math.h prob Dave Brennan
  1999-11-03  8:27 ` Art Haas
@ 1999-11-03  8:28 ` haha
  1999-11-30 23:28   ` haha
  1999-11-03  8:30 ` Ffoobarr
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: haha @ 1999-11-03  8:28 UTC (permalink / raw)
  To: help-gcc

:>I am fairly new to C programming, especially to using gcc, so I am sorry
:>if my question is a bit simple!
	<SNIP>
:>and try to compile it using,
:>gcc -o test mattest.c
:>I get the following error:
:>Undefined                       first referenced
:> symbol                             in file
:>sqrt                                /var/tmp/cc9Eay_r1.o

You need to link the math library to your program with the -lm option. I
recommend you don't call your program 'test', since this is a command:
gcc -o math_test mattest.c -lm

Haha!

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: math.h prob
  1999-11-03  8:21 math.h prob Dave Brennan
  1999-11-03  8:27 ` Art Haas
  1999-11-03  8:28 ` haha
@ 1999-11-03  8:30 ` Ffoobarr
  1999-11-30 23:28   ` Ffoobarr
  1999-11-03  9:51 ` Dave Brennan
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Ffoobarr @ 1999-11-03  8:30 UTC (permalink / raw)
  To: help-gcc

Dave Brennan wrote:
>I am fairly new to C programming, especially to using gcc, so I am sorry
>if my question is a bit simple!
>I am using gcc 2.8.1 on a Sun workstation (solaris 2.7).
>I am having problems using maths functions. e.g if I have the program
>
>#include <stdio.h>
>#include <math.h>
>main()
>{
>double x;
>scanf("%lf",&x);
>printf("x = %lf",sqrt(x));
>}
>
>and try to compile it using,
>gcc -o test mattest.c
>I get the following error:
>Undefined                       first referenced
> symbol                             in file
>sqrt                                /var/tmp/cc9Eay_r1.o
>ld: fatal: Symbol referencing errors. No output written to test

Hi Dave.  This is a very, very frequently asked question.  The problem
is the math library is separate from the regular C library and you need
to explicitly link in the math library, using the -lm flag.  So, in your
case, the correct compiler command would be:

gcc -o mattest mattest.c -lm

Also, please take note of the fact that I changed the name of the
executable to something other than test.  There is already a command
called 'test' that you will problably end up running instead of your
'test'.

And... I hope you don't mind a few style suggestions:  Explicitly declare
main's return type and parameters as 'int main(void)' instead of just
'main()', and add a 'return 0;' line before the closing brace.
In your printf() format string, you should use '%f' instead of '%lf'.
Your scanf() format specifier is fine, but you might want to consider
checking the return value of scanf() for failure in case the user didn't
type something that could be converted to a floating-point value (like
ABCDEFG instead of 3.14).

Hope this helps!

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: math.h prob
  1999-11-03  8:21 math.h prob Dave Brennan
                   ` (2 preceding siblings ...)
  1999-11-03  8:30 ` Ffoobarr
@ 1999-11-03  9:51 ` Dave Brennan
  1999-11-30 23:28   ` Dave Brennan
  1999-11-03 13:25 ` Rick Dearman
  1999-11-30 23:28 ` Dave Brennan
  5 siblings, 1 reply; 12+ messages in thread
From: Dave Brennan @ 1999-11-03  9:51 UTC (permalink / raw)
  To: help-gcc

Thanks for the tip guys

Cheers

Dave B

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: math.h prob
  1999-11-03  8:21 math.h prob Dave Brennan
                   ` (3 preceding siblings ...)
  1999-11-03  9:51 ` Dave Brennan
@ 1999-11-03 13:25 ` Rick Dearman
  1999-11-30 23:28   ` Rick Dearman
  1999-11-30 23:28 ` Dave Brennan
  5 siblings, 1 reply; 12+ messages in thread
From: Rick Dearman @ 1999-11-03 13:25 UTC (permalink / raw)
  To: help-gcc

> #include <stdio.h>
> #include <math.h>
> 
> main()
You know main returns an int, as a good practice you should use:
int 
main(void)
> {
> 
> double x;
> 
> scanf("%lf",&x);
Really you should try to use fgets() and sscanf() here, try downloading
the comp.lang.c FAQ from:
http://www.eskimo.com/~scs/C-faq/top.html     ( comp.lang.c FAQ )

> 
> printf("x = %lf",sqrt(x));
> 
you should put a return statement in here:
	return 0; /* This is the normal return for sucess
		  ** or you can use exit but you would have 
		  ** to include <stdlib.h> as well.
	          */
> }
> 
> and try to compile it using,
> 
> gcc -o test mattest.c
> 
> I get the following error:

<snip>

You forgot to link the math library. As everyone else has said.

:-)

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: math.h prob
  1999-11-03  8:27 ` Art Haas
@ 1999-11-30 23:28   ` Art Haas
  0 siblings, 0 replies; 12+ messages in thread
From: Art Haas @ 1999-11-30 23:28 UTC (permalink / raw)
  To: help-gcc

Dave Brennan <9147261b@clinmed.gla.ac.uk> writes:

> Hi,
> 
> I am having problems using maths functions. e.g if I have the program
> 
> #include <stdio.h>
> #include <math.h>
> 
> main()
> {
> double x;
> scanf("%lf",&x);
> printf("x = %lf",sqrt(x));
> }
> 
> and try to compile it using,
> 
> gcc -o test mattest.c
> 
> I get the following error:
> 
> Undefined                       first referenced
>  symbol                             in file
> sqrt                                /var/tmp/cc9Eay_r1.o
> ld: fatal: Symbol referencing errors. No output written to test
> 

You need to link in the math library ...

% gcc -o test mattest.c -lm

-- 
###############################
# Art Haas
# (713) 689-2417
###############################

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: math.h prob
  1999-11-03  8:30 ` Ffoobarr
@ 1999-11-30 23:28   ` Ffoobarr
  0 siblings, 0 replies; 12+ messages in thread
From: Ffoobarr @ 1999-11-30 23:28 UTC (permalink / raw)
  To: help-gcc

Dave Brennan wrote:
>I am fairly new to C programming, especially to using gcc, so I am sorry
>if my question is a bit simple!
>I am using gcc 2.8.1 on a Sun workstation (solaris 2.7).
>I am having problems using maths functions. e.g if I have the program
>
>#include <stdio.h>
>#include <math.h>
>main()
>{
>double x;
>scanf("%lf",&x);
>printf("x = %lf",sqrt(x));
>}
>
>and try to compile it using,
>gcc -o test mattest.c
>I get the following error:
>Undefined                       first referenced
> symbol                             in file
>sqrt                                /var/tmp/cc9Eay_r1.o
>ld: fatal: Symbol referencing errors. No output written to test

Hi Dave.  This is a very, very frequently asked question.  The problem
is the math library is separate from the regular C library and you need
to explicitly link in the math library, using the -lm flag.  So, in your
case, the correct compiler command would be:

gcc -o mattest mattest.c -lm

Also, please take note of the fact that I changed the name of the
executable to something other than test.  There is already a command
called 'test' that you will problably end up running instead of your
'test'.

And... I hope you don't mind a few style suggestions:  Explicitly declare
main's return type and parameters as 'int main(void)' instead of just
'main()', and add a 'return 0;' line before the closing brace.
In your printf() format string, you should use '%f' instead of '%lf'.
Your scanf() format specifier is fine, but you might want to consider
checking the return value of scanf() for failure in case the user didn't
type something that could be converted to a floating-point value (like
ABCDEFG instead of 3.14).

Hope this helps!

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: math.h prob
  1999-11-03 13:25 ` Rick Dearman
@ 1999-11-30 23:28   ` Rick Dearman
  0 siblings, 0 replies; 12+ messages in thread
From: Rick Dearman @ 1999-11-30 23:28 UTC (permalink / raw)
  To: help-gcc

> #include <stdio.h>
> #include <math.h>
> 
> main()
You know main returns an int, as a good practice you should use:
int 
main(void)
> {
> 
> double x;
> 
> scanf("%lf",&x);
Really you should try to use fgets() and sscanf() here, try downloading
the comp.lang.c FAQ from:
http://www.eskimo.com/~scs/C-faq/top.html     ( comp.lang.c FAQ )

> 
> printf("x = %lf",sqrt(x));
> 
you should put a return statement in here:
	return 0; /* This is the normal return for sucess
		  ** or you can use exit but you would have 
		  ** to include <stdlib.h> as well.
	          */
> }
> 
> and try to compile it using,
> 
> gcc -o test mattest.c
> 
> I get the following error:

<snip>

You forgot to link the math library. As everyone else has said.

:-)

^ permalink raw reply	[flat|nested] 12+ messages in thread

* math.h prob
  1999-11-03  8:21 math.h prob Dave Brennan
                   ` (4 preceding siblings ...)
  1999-11-03 13:25 ` Rick Dearman
@ 1999-11-30 23:28 ` Dave Brennan
  5 siblings, 0 replies; 12+ messages in thread
From: Dave Brennan @ 1999-11-30 23:28 UTC (permalink / raw)
  To: help-gcc

Hi,

I am fairly new to C programming, especially to using gcc, so I am sorry
if my question is a bit simple!

I am using gcc 2.8.1 on a Sun workstation (solaris 2.7).

I am having problems using maths functions. e.g if I have the program

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

main()
{

double x;

scanf("%lf",&x);

printf("x = %lf",sqrt(x));


}

and try to compile it using,

gcc -o test mattest.c

I get the following error:

Undefined                       first referenced
 symbol                             in file
sqrt                                /var/tmp/cc9Eay_r1.o
ld: fatal: Symbol referencing errors. No output written to test

Does anyone know why this is happening?

Thanks for the help.


Dave Brennan

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: math.h prob
  1999-11-03  9:51 ` Dave Brennan
@ 1999-11-30 23:28   ` Dave Brennan
  0 siblings, 0 replies; 12+ messages in thread
From: Dave Brennan @ 1999-11-30 23:28 UTC (permalink / raw)
  To: help-gcc

Thanks for the tip guys

Cheers

Dave B

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: math.h prob
  1999-11-03  8:28 ` haha
@ 1999-11-30 23:28   ` haha
  0 siblings, 0 replies; 12+ messages in thread
From: haha @ 1999-11-30 23:28 UTC (permalink / raw)
  To: help-gcc

:>I am fairly new to C programming, especially to using gcc, so I am sorry
:>if my question is a bit simple!
	<SNIP>
:>and try to compile it using,
:>gcc -o test mattest.c
:>I get the following error:
:>Undefined                       first referenced
:> symbol                             in file
:>sqrt                                /var/tmp/cc9Eay_r1.o

You need to link the math library to your program with the -lm option. I
recommend you don't call your program 'test', since this is a command:
gcc -o math_test mattest.c -lm

Haha!

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~1999-11-30 23:28 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-11-03  8:21 math.h prob Dave Brennan
1999-11-03  8:27 ` Art Haas
1999-11-30 23:28   ` Art Haas
1999-11-03  8:28 ` haha
1999-11-30 23:28   ` haha
1999-11-03  8:30 ` Ffoobarr
1999-11-30 23:28   ` Ffoobarr
1999-11-03  9:51 ` Dave Brennan
1999-11-30 23:28   ` Dave Brennan
1999-11-03 13:25 ` Rick Dearman
1999-11-30 23:28   ` Rick Dearman
1999-11-30 23:28 ` Dave Brennan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).