Simple Question: sqrt()

Simple Question: sqrt()

Post by Adam Hutto » Tue, 11 Feb 1997 04:00:00



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

double a,b;

main()
{

  a = 1;
  b = sqrt(a);

Quote:}  

When I compile the program above:  

cc temp.c

I get the following response:

ld:
Unresolved:
sqrt

For some reason I cannot use the sqrt() function.  I'm sure the answer is
simple. ( I hope ) Any help would be greatly appreciated.

System: DEC ALPHAstation 255
        Digital UNIX

Thanks,

Adam Hutton

 
 
 

Simple Question: sqrt()

Post by Frank Huben » Wed, 12 Feb 1997 04:00:00


Use the following command to compile:  cc temp.c -lm

The -l makes the libm available where sqrt() is defined.


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

> double a,b;

> main()
> {

>   a = 1;
>   b = sqrt(a);

> }

> When I compile the program above:

> cc temp.c

> I get the following response:

> ld:
> Unresolved:
> sqrt

--
Frank Hubeny

Wheaton, IL 60187

 
 
 

Simple Question: sqrt()

Post by Stephan Zimmerma » Thu, 13 Feb 1997 04:00:00


[Posted and mailed]



Quote:> When I compile the program above:  

> cc temp.c

> I get the following response:

> ld:
> Unresolved:
> sqrt

You have to link your program with the math-library (m).

cc temp.c -o temp -lm

This will help (I think).

Bye Stephan.

 
 
 

Simple Question: sqrt()

Post by Randy Kaelb » Thu, 13 Feb 1997 04:00:00



> When I compile the program above:  
> cc temp.c

You need to link in the math lib. try:

cc -lm temp.c

and all will be right with the world.
--

DARS Programmer/Analyst, Miami University, Oxford, OH 45056 USA
http://avian.dars.muohio.edu/~randy/

 
 
 

Simple Question: sqrt()

Post by Stephen Dempse » Tue, 18 Feb 1997 04:00:00


You need to link with the standard math library libm.a.  To link with
it, use -lm when you compile (i.e. cc temp.c -lm).  You only need to
specify the m because the lib and the .a are assumed.  Thus to link
to an arbitrary library libX.a, use -lX.

This should solve your problem!


> When I compile the program above:

> cc temp.c

> I get the following response:

> ld:
> Unresolved:
> sqrt

--
                   ^^^
                   0 0
------------ooOO----O----OOoo------------
             Stephen Dempsey
 Lockheed Martin Gov't Electronic Systems
-----------------------------------------
 
 
 

Simple Question: sqrt()

Post by David Thomas Richard Giv » Thu, 20 Feb 1997 04:00:00




[...]

Quote:>cc temp.c

>I get the following response:

>ld:
>Unresolved:
>sqrt

[...]

You need to link in with the maths library.

        cc temp.c -lm

It's also a good idea to add the output file name, as well, unless you
like your executables to be called a.out.

        cc -o temp temp.c -lm

This will create an executable called `temp'.

--
------------------- http://www-hons-cs.cs.st-and.ac.uk/~dg --------------------
   If you're up against someone more intelligent than you are, do something
    totally insane and let him think himself to death.  --- Pyanfar Chanur
---------------- Sun-Earther David Daton Given of Lochcarron ------------------

 
 
 

Simple Question: sqrt()

Post by » Fri, 21 Feb 1997 04:00:00


Quote:> Why is it designed that way that simple #including of <math.h>
> is not enough but I have to specify "-lm" on the command line?

Because unix executable formats are stupid.

Other operating systems have had executable formats that included loader
directives such as which libraries to search. Unix is definitely an
improvement on 1968-era operating systems, but after that......

One alternative that could work is for compilers to write the optional
loader directives files that ld already supports. But real programmers
don't need some * machine getting in the way of their work.

--
His hands are cold and heart is hot     |   S M Ryan, Cupertino CA

The shadowed glooms are shapeless dooms.| or risk losing the mail.
He dreads the lessons demon-taught.     |  I don't use no smileys.

 
 
 

Simple Question: sqrt()

Post by Jeremy Nels » Fri, 21 Feb 1997 04:00:00



Quote:>Why is it designed that way that simple #including of <math.h>
>is not enough but I have to specify "-lm" on the command line?
>When I #include <stdio.h> or <stdlib.h>, compiler doesn't seem
>to need any "-l[whatever]" switches. Can compilers be redesigned
>to automatically "-lm" when preprocessor sees #include <math.h>?

To speak abstractly, there is nothing at all that requires me to use
the bundled compiler[1] on a given system:  Nor even that the system come
with a bundled compiler[1]:  To that extent, header files are provided by
the operating system as a guide for programs to have proper interfacing
with system-provided functionality (either through bundled libraries or
system calls).  This is the general description of a software development
kit (SDK).  Just as many windows vendors sell you an "SDK" to their product,
but dont sell a compiler along with it, unix comes bundled with an "SDK"
to that system, but may or may not come with a compiler.  To that extent,
an "SDK" is independant of the compiler.  Does a windows compiler
automatically know to link in foo.obj that binds to bar.dll simply becuase
i do #include <whizard.h>?  To that same extent, a unix compiler cannot
know that on every system where you #include <math.h> you is a library
called libm.

What it comes down to is that you cannot expect a unix compiler[2], such
as gnu cc to have intimite knowledge of every aspect of your SDK.  Some-
times, you just have to help your compiler out a little bit.  Its the same
in windows and dos, except that those compilers[1] have intimite knowledge
of their own shipped SDK, since it is more or less exclusive to that
compiler[2], meaning each compiler wants to have its own SDK.  Ugh.

Unix compilers[2] (most at least) dont create executables, and most unix
linkers dont ever see your raw source code.  You can mix and match
compilers[2] and linkers pretty much at will, and people do.

Quote:>Would be much simpler, especially for newbies like me, who yesterday
>spent about an hour trying to figure out why a certain X program
>didn't compile despite I #included everything it needed in
>/usr/X11R6/include/X11: I found out I needed to add "-l/usr/X11R6/lib".
>It would have been very nice if gcc could see my #includes
>and add all Xlib automatically. Just sounds a little bit confusing
>to me.

Its a hard lesson to learn, but i bet you wont have to make it again,
becuase youve already learned.  And when you move to another platform
that also has this quirk (becuase it is allowed by the standard), you
will know what to do without being lost.

Sometimes (not always! ;-) the most unforgiving systems are the most helpful
in banging your code into shape.

Jeremy

-- notes ---
compiler[1] -- "That which turns source code into executables, which
includes also at least the concepts of the pre-processor, the assembler,
and the linker.  It is (collectively) all of the programs that are
needed to have a fully functioning development system."

compiler[2] -- "That part of the development system which converts
source code into assembly code, except on digital systems where it
converts source code into object code.  It is (disinctly) that part
of the development system that converts your code into some kind of
system-dependant representation."

 
 
 

Simple Question: sqrt()

Post by Frank Huben » Fri, 21 Feb 1997 04:00:00



> Why is it designed that way that simple #including of <math.h>
> is not enough but I have to specify "-lm" on the command line?
> When I #include <stdio.h> or <stdlib.h>, compiler doesn't seem
> to need any "-l[whatever]" switches. Can compilers be redesigned
> to automatically "-lm" when preprocessor sees #include <math.h>?

   The reason would seem to be that each of the items does something
different:

   The #include simply inserts text into the source program.  To see
what is being inserted compile using the -P option and check the .i file
that is output.  Include files are a convenience that comes at the price
of including a lot of stuff that is not necessary for the running of
particular program.

   The -l tells the linker which libraries to search and in which order
to search them.  By default one does not have to mention -lc in order to
get the standard C library.  It is available by default and is searched
after all other libraries mentioned with -l.  So to override a function
in the standard library, just put a new version of it in a new library
and use -l to identify that new library to the linker.

   An additional but related issue is the library path which can be
stated as either -L on the command line or with the environment variable
LD_LIBRARY_PATH.  If there are two different versions of the same
library, this path will determine which version of the library is used.
To see which version is linked, run ldd on the executable.  Change the
library path and a new version (of a shared object) would be linked
dynamically without recompiling the program.

--
Frank Hubeny

Wheaton, IL 60187

 
 
 

Simple Question: sqrt()

Post by Bradley Thomps » Sat, 22 Feb 1997 04:00:00


Quote:

> Why is it designed that way that simple #including of <math.h>
> is not enough but I have to specify "-lm" on the command line?
> When I #include <stdio.h> or <stdlib.h>, compiler doesn't seem
> to need any "-l[whatever]" switches. Can compilers be redesigned
> to automatically "-lm" when preprocessor sees #include <math.h>?
> Would be much simpler, especially for newbies like me, who yesterday
> spent about an hour trying to figure out why a certain X program
> didn't compile despite I #included everything it needed in
> /usr/X11R6/include/X11: I found out I needed to add "-l/usr/X11R6/lib".
> It would have been very nice if gcc could see my #includes
> and add all Xlib automatically. Just sounds a little bit confusing
> to me.

gcc is not just one program.  The compiler (which I think is actually in
gcc) compiles the program after it is preprocessed by cpp, the only program
that sees the includes.  After the compiler compiles the (#include-free)
files, it is assembled (by gas) and linked by ld.  ld and cpp have no
way of communicating this information to each other, and adding it would
be too much effort to warrant this minor convenience.  To answer the
other question, every C program is linked with the standard C library, which
contains the code for stdio.h, stdlib.h, and a bunch of others.  Many other
routines (fork(), kill(), etc.) are built into the system and do not
need a library at all.
                                                      --Brad
--

__________________________________________________________
 Bradley Thompson          http://www.ids.net/~bradley

 
 
 

1. A simple question deserving a simple answer

: What does Unix (Specifically Linux) look like?  

A bunch of ones and zeros ;-)

: Is it like Dos? Windows?

No, thank God.

: Something else?

Yes.

There's a simple answer...

--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming

2. Colorado Trakker 250

3. a simple sed question ( there all simple :> )

4. changing sparc linux video mode?

5. Simple question from simple user

6. Mach 64 + XFree: What's the fuss?

7. Simple FTP question (very simple)

8. Cirrus Logic CL-GD 6410 support for Linux -- anyone got a patch?

9. simple question about simple code

10. Simple question for us simple minded.

11. Simple question - simple answer?

12. Fast sqrt of int algorithm?

13. problem with sqrt and errno, FreeBSD release 4.3