473,378 Members | 1,658 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,378 software developers and data experts.

trouble sqrt in gcc

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
Nov 13 '05 #1
7 6751
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
Nov 13 '05 #2
"Aric" <mo*******@beethoven.com> wrote in message
news:40**************************@posting.google.c 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.
Nov 13 '05 #3
"Greg P." <no@spam.sam> 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.
Nov 13 '05 #4
"Simon Biber" <sb****@optushome.com.au> wrote in message
news:3f***********************@news.optusnet.com.a 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
Nov 13 '05 #5
"Greg P." <no@spam.sam> wrote:
"Simon Biber" <sb****@optushome.com.au> 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.
Nov 13 '05 #6
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
Nov 13 '05 #7
"Simon Biber" <sb****@optushome.com.au> wrote in message
news:3f***********************@news.optusnet.com.a 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
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: csumner | last post by:
I am trying to use the haversine function to find the distance between two points on a sphere, specifically two zip codes in my database. I'm neither horribly familiar with SQL syntax nor math...
13
by: Martijn van den Branden | last post by:
Hi, sorry if this message gets posted twice, i seem to have some problem posting to newsgroups. i've rewritten some loop in matlab using the mex interface method in c, and i've achieved a...
13
by: Michael McNeil Forbes | last post by:
I would like to write a module that provides some mathematical functions on top of those defined in math, cmath etc. but I would like to make it work with "any" type that overloads the math...
12
by: Santosh Krisnan | last post by:
hello all, I fiddled with BASIC in the early 90s but left it at that. Now I am trying to learn C. I tried to solve an exercise in my book, but it failes to compile. Can anyone tell me what the...
6
by: 3than7 | last post by:
I am writing an application to solve Pythagorean Theorum Problems. This is on my own time, i am using a book to learn c++, and after doing a fahrenheit to celsuis program from that book, i wanted...
13
by: siggi | last post by:
Hi all, this is a newbie question on : Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32 PC with WinXP In http://www.python.org/doc/2.3.5/lib/module-math.html I read:
30
by: copx | last post by:
I am writing a program which uses sqrt() a lot. A historically very slow function but I read on CPUs with SSE support this is actually fast. Problem: C's sqrt() (unlike C++ sqrt()) is defined to...
13
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.