Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > trouble sqrt in gcc

Reply
Thread Tools

trouble sqrt in gcc

 
 
Aric
Guest
Posts: n/a
 
      08-26-2003
Hi,

I'm a bit new to programming in general, really I've just picked it up
as a hobby, and so I've started by reading "Practical C Programming"
by Steve Oualline. Things have been going fine in the book until I
reached exercise 6-1 in the book, which reads:

Write a program to find the square of the distance between two points.
(For a more advanced problem, find the actual distance. This problem
involves using the standard function sqrt. Use your help system to
find out more about how to use this function.)

I did fine with the basic problem, but I can't seem to compile when I
try to use sqrt. Here is my code:

#include <stdio.h>
#include <math.h>
char coordinate[50]; /* input string */
int myx1; /* first x coordinate */
int myx2; /* second x coordinate */
int myy1; /* first y coordinate */
int myy2; /* second y coordinate */
double sqrdist; /* distances square */
double distance; /* actual distance between two points */
int absxsquare; /* absolute value of (x1 + x2) ^2 */
int absysquare; /* absolute value of (y1 + y2) ^2 */

int main()
{
printf("Enter the X value of the first coordinate: ");
fgets(coordinate, sizeof(coordinate), stdin);
sscanf(coordinate, "%d", &myx1);

printf("Enter the Y value of the first coordinate: ");
fgets(coordinate, sizeof(coordinate), stdin);
sscanf(coordinate, "%d", &myy1);

printf("Enter the X value of the second coordinate: ");
fgets(coordinate, sizeof(coordinate), stdin);
sscanf(coordinate, "%d", &myx2);

printf("Enter the Y value of the second coordinate: ");
fgets(coordinate, sizeof(coordinate), stdin);
sscanf(coordinate, "%d", &myy2);

absxsquare = fabs ((myx1 - myx2) * (myx1 - myx2));
absysquare = fabs ((myy1 - myy2) * (myy1 - myy2));
sqrdist = absxsquare + absysquare;
distance = sqrt(sqrdist);

printf("\nThe square of the distance between those two points
is %f",sqrdist);
printf("\nThe actual difference is %f\n",distance);

return(0);
}

And here are my compile errors:

/tmp/ccQe7XsT.o: In function `main':
/home/madducks/cprogs/ex6-1.c:34: undefined reference to `sqrt'
collect2: ld returned 1 exit status

I thought I had implemented the sqrt as the man page for sqrt
explained, but as you might guess by my code, I'm more than a bit
lost. Any help would be greatly appreciated.

Thanks,
Aric
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-26-2003
Aric wrote:

> #include <math.h>


> And here are my compile errors:
>
> /tmp/ccQe7XsT.o: In function `main':
> /home/madducks/cprogs/ex6-1.c:34: undefined reference to `sqrt'
> collect2: ld returned 1 exit status
>
> I thought I had implemented the sqrt as the man page for sqrt
> explained, but as you might guess by my code, I'm more than a bit
> lost. Any help would be greatly appreciated.


http://www.eskimo.com/~scs/C-faq/q14.3.html
http://www.eskimo.com/~scs/C-faq/q13.25.html

--
pete
 
Reply With Quote
 
 
 
 
Greg P.
Guest
Posts: n/a
 
      08-26-2003
"Aric" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
| Hi,
Hello

<snip>
| I did fine with the basic problem, but I can't seem to compile when I
| try to use sqrt. Here is my code:
|
| #include <stdio.h>
| #include <math.h>
| char coordinate[50]; /* input string */
| int myx1; /* first x coordinate */
| int myx2; /* second x coordinate */
| int myy1; /* first y coordinate */
| int myy2; /* second y coordinate */
| double sqrdist; /* distances square */
| double distance; /* actual distance between two points */
| int absxsquare; /* absolute value of (x1 + x2) ^2 */
| int absysquare; /* absolute value of (y1 + y2) ^2 */

I understand that this is just an exercise, but in general you should
reserve your global declarations for static (extern, etc) related data. Is
Mr. Oualline giving examples with global vars in his code? tssk tssk

<snip>
| And here are my compile errors:
|
| /tmp/ccQe7XsT.o: In function `main':
| /home/madducks/cprogs/ex6-1.c:34: undefined reference to `sqrt'
| collect2: ld returned 1 exit status

You need to link against the math library in GCC. Some compilers don't
require you to do so, as they incorporate most of the standard C runtime
library into a single(or a couple) statically linked libraries that get
added to your code, increasing its footprint(executable size). Now what if,
perhaps, you did not use the math functions and the compiler still linked
the C runtime (which has math in it) to your application, causing unneeded
size? This is not always the case for some efficient compilers (usually
needing extra switches), but after a while you will appreciate being able to
select which specific libraries you want to be added and which to be
omitted.

You need to compile as such.

gcc -o ex6-1.c -lm ex6.c (add whatever extra flags/switches you prefer)

The -lm option is a passive switch that waits to be sent to the linker
telling it to add the math library. For fun (at least I consider it to be),
list your /usr/lib directory:

cd ~yourusername
ls -l /usr/lib | less

or...

ls -l /usr/lib > mylibraries.list

And then read them with less (less mylibraries.list).

You will see some common names in there relating to the C runtime library
headers (usually with the same prefixed forename) so you can you know which
libraries to add to your linker resolution when the time comes.

Note that when if you have a file called "libSOMETHING.so" you would only
have to pass the flag (-lSOMETHING to gcc rather than adding the prefixed
"lib" or the extension).

Happy hacking!

Greg P.


 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      08-27-2003
"Greg P." <(E-Mail Removed)> wrote:
> You need to link against the math library in GCC. Some compilers don't
> require you to do so, as they incorporate most of the standard C runtime
> library into a single(or a couple) statically linked libraries that get
> added to your code, increasing its footprint(executable size). Now what
> if, perhaps, you did not use the math functions and the compiler still
> linked the C runtime (which has math in it) to your application, causing
> unneeded size? This is not always the case for some efficient compilers
> (usually needing extra switches), but after a while you will appreciate
> being able to select which specific libraries you want to be added and
> which to be omitted.


This library issue is entirely orthogonal to what compiler you use. It has
nothing to do with GCC. It's a feature of many Unixes, but exists no
matter what compiler you use on that system. It also doesn't apply when
you use GCC on a system which does include math functions in the main
library.

> You need to compile as such.
>
> gcc -o ex6-1.c -lm ex6.c (add whatever extra flags/switches you prefer)


You want to output the binary executable to a .c file? That's not clever.
Either leave it with no extension (common on Unixes) or with a .EXE
extension (common on DOS,Windows,OS/2,etc).

> The -lm option is a passive switch that waits to be sent to the linker
> telling it to add the math library.


Not exactly passive; it is just sent to the linker but not to the
preprocessor or compiler. The front-end 'gcc' is not a compiler.

On my system it runs three programs to execute your command. With
massively simplified arguments, they are:
Compiler: cc1 ex6.c -o FirstTemp
Assembler: as FirstTemp -o SecondTemp
Linker: collect2 SecondTemp -o ex6-1.c -lm

--
Simon.


 
Reply With Quote
 
Greg P.
Guest
Posts: n/a
 
      08-27-2003
"Simon Biber" <(E-Mail Removed)> wrote in message
news:3f4bfd71$0$28121$(E-Mail Removed) u...
| This library issue is entirely orthogonal to what compiler you use. It has
| nothing to do with GCC. It's a feature of many Unixes, but exists no
| matter what compiler you use on that system. It also doesn't apply when
| you use GCC on a system which does include math functions in the main
| library.

True, however, I was replying to the poster regarding the GNU Compiler
Collection, not *any* compiler on *NIX. If the poster said he was using dmc
on a win32 platform asking about optimizations, I would reply with
information regarding the "-o" switches for dmc, regardless of the fact that
most compilers support this.

| > gcc -o ex6-1.c -lm ex6.c (add whatever extra flags/switches you prefer)
|
| You want to output the binary executable to a .c file? That's not clever.

That was a mis-type on my behalf, posters do not always reply with 100%
perfect messages. I am sure that the OP is smart enough to understand that
the output name should be otherwise


 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      08-27-2003
"Greg P." <(E-Mail Removed)> wrote:
> "Simon Biber" <(E-Mail Removed)> wrote in message
> | This library issue is entirely orthogonal to what compiler you use.
> | It has nothing to do with GCC. It's a feature of many Unixes, but
> | exists no matter what compiler you use on that system. It also
> | doesn't apply when you use GCC on a system which does include math
> | functions in the main library.
>
> True, however, I was replying to the poster regarding the GNU
> Compiler Collection, not *any* compiler on *NIX.


The OP did not mention Unix at all; you reasonably assumed that from the
symptoms of his problem. But you didn't make it clear that the problem
has nothing to do with the compiler, and rather is a feature of the C
library of the OP's operating system.

You said:
> > You need to link against the math library in GCC.


The word 'you' can have two meanings, I took at as a general 'one needs
to' rather than 'in your particular case, you need to'. In the general
case, this advice is flat out wrong. Assuming you mean the specific, ...

> > Some compilers don't require you to do so, as they incorporate most
> > of the standard C runtime library into a single(or a couple)
> > statically linked libraries that get added to your code, increasing
> > its footprint(executable size).


Remember that compilers and libraries are completely separate things.
Can you name one compiler on his platform that doesn't require him to
do so, as it automatically incorporates statically linked libraries?

> If the poster said he was using dmc on a win32 platform asking about
> optimizations, I would reply with information regarding the "-o"
> switches for dmc, regardless of the fact that most compilers support
> this.


Actually, you should redirect it to an appropriate group since asking
about particular compilers is off-topic in comp.lang.c.

--
Simon.


 
Reply With Quote
 
Tim Prince
Guest
Posts: n/a
 
      08-27-2003
Aric wrote:

>
> #include <stdio.h>
> #include <math.h>
>
>
> I thought I had implemented the sqrt as the man page for sqrt
> explained, but as you might guess by my code, I'm more than a bit
> lost. Any help would be greatly appreciated.
>

A little more reading in your compiler and platform documentation. You
must either specify linkage with the math library, according to your
platform (likely guess, as the C faq tells you, -lm), or tell gcc to use
in-line sqrt instructions exclusively (-ffast-math), thereby disabling
<errno.h> processing. These can only be guesses, as they depart from the
topic of this NG, and you haven't specified your target platform.
--
Tim Prince
 
Reply With Quote
 
Greg P.
Guest
Posts: n/a
 
      08-27-2003
"Simon Biber" <(E-Mail Removed)> wrote in message
news:3f4c49db$0$28117$(E-Mail Removed) u...
| The OP did not mention Unix at all; you reasonably assumed that from the
| symptoms of his problem. But you didn't make it clear that the problem
| has nothing to do with the compiler, and rather is a feature of the C
| library of the OP's operating system.

I "assumed" due to this statement from the OP:
>And here are my compile errors:


>/tmp/ccQe7XsT.o: In function `main':

^
>/home/madducks/cprogs/ex6-1.c:34: undefined reference to `sqrt'

^
>collect2: ld returned 1 exit status


If you search for other posts regarding gcc use on this newsgroup, the
replies also "assume" that the user is running a *nix variant (unless they
otherwise specify MYSYS or Cygwin)

| Remember that compilers and libraries are completely separate things.
| Can you name one compiler on his platform that doesn't require him to
| do so, as it automatically incorporates statically linked libraries?

The Intel Compiler for Linux automatically links the math lib


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: sqrt() double trouble Tim Prince C Programming 14 06-07-2008 03:46 AM
Re: sqrt() double trouble Martin Ambuhl C Programming 0 06-06-2008 09:04 AM
How to use sqrt() function with gcc? John C Programming 3 08-04-2006 05:02 AM
gcc 2.95 and gcc 3.2 gouqizi.lvcha@gmail.com C++ 8 03-16-2005 02:34 AM
C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1 Kevin P. Fleming C Programming 2 11-06-2003 05:15 AM



Advertisments