public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: fubar@example.com (Ffoobarr)
To: help-gcc@gnu.org
Subject: Re: math.h prob
Date: Wed, 03 Nov 1999 08:30:00 -0000	[thread overview]
Message-ID: <slrn820ls9.4c9.fubar@deepspace4.mdac.most> (raw)
In-Reply-To: <38204719.FD2DC371@clinmed.gla.ac.uk>

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!

WARNING: multiple messages have this Message-ID
From: fubar@example.com (Ffoobarr)
To: help-gcc@gnu.org
Subject: Re: math.h prob
Date: Tue, 30 Nov 1999 23:28:00 -0000	[thread overview]
Message-ID: <slrn820ls9.4c9.fubar@deepspace4.mdac.most> (raw)
Message-ID: <19991130232800.ug2LI5J08hjAskiFzltnQ-G6wJMsbn717lAz3saYmpI@z> (raw)
In-Reply-To: <38204719.FD2DC371@clinmed.gla.ac.uk>

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!

  parent reply	other threads:[~1999-11-03  8:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-11-03  8:21 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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=slrn820ls9.4c9.fubar@deepspace4.mdac.most \
    --to=fubar@example.com \
    --cc=help-gcc@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).