trouble sqrt in gcc

A

Aric

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
 
P

pete

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
 
G

Greg P.

| 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.
 
S

Simon Biber

Greg P. said:
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
 
G

Greg P.

| 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
 
S

Simon Biber

Greg P. said:
| 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:
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, ...

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.
 
T

Tim Prince

Aric said:
#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.
 
G

Greg P.

| 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top