EmbDev.net

Forum: ARM programming with GCC/GNU tools Newbe : Understanding -lm math library linkage


Author: Joe Gold (joeg)
Posted on:

Rate this post
0 useful
not useful
Hi All,

To use math.h    where do I put the  -lm in a WinArm make file.

How does Make and the compiler know where math.h lives?

Where can I get the descriptions of the math.h library functions?


Thanks in advance.

Joe

Author: Clifford Slocombe (clifford)
Posted on:

Rate this post
0 useful
not useful
I apologise if the following tells you a lot you already know, but I
sense some confusion in your question, so I'm pretty much going to give
you the whole story.

First be clear that math.h is NOT the math library. It is merely the
header file that contains prototype function declarations for the code
in the math library. The math library itself is the file libm.a.

Now the compiler needs the header file so that it knows the signature of
the math functions. When the compiler encounters a math function, it
checks that the signature matches the call, then inserts an unresolved
link in the object code - a kind of place-holder for the actual function
call at an as yet unknown address (note I am referring to functions, but
it is similar for global data items as well). The linker later takes all
the object modules from separately compiled sources and resolves
inter-module links between them, then to resolve any remaining links it
searches the libraries in they order they are listed on the linker
command line.

So to answer your questions:

> To use math.h    where do I put the  -lm in a WinArm make file.
-lm is a linker option. The option is actually -l<libraryname>, the
library is libm.a but the GNU linker will insert the lib prefix and .a
extension automatically. Now where in your makefile to place the option
will depend entirely on how the makefile is constructed - makefiles can
be as complex and infinitely varied as the code they build, however
conventionally a macro LDFLAGS is defined for linker options.
Alternatively find how and where ld (the linker) is invoked in the
makefile, and place it there or in whatever appropriate macro is used
there.

> How does Make and the compiler know where math.h lives?
The compiler searches for header files in a number of ways. Compiler
options of the form -I<path> specify paths that will be searched for
header files. These paths are searched in the order listed on the
command line. In GCC, include file paths can also be specified by the
following environment variables:

CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH

each path being separated by a semi-colon on Windows, and a colon on
most other platforms. If the file is included using "file" rather than
<file> the compiler will look in the sourcefiles directory first.
Finally it is possible to specify the path explicitly, either direct or
relative, within the #include directive itself.

A typical makefile will have a macro that specifies the -I paths.

The linker has a -L<path> option which serves the same purpose as the
compiler's -I<path> but for library file searches. Similarly there is an
environment variable "LIBRARY_PATH".

Note that other compilers may use different options and environment
variables t0 do the same thing. Using environment variables is not a
good idea for cross-compilers since it is likely to conflict with any
native compiler you may be using.

> Where can I get the descriptions of the math.h library functions?
It is a C standard library - the references are numerous. The libm
manual for the Newlib library is provided with WinARM in the documents
folder, or download from: ftp://sources.redhat.com/pub/newlib/index.html
Other sources include:
http://www.cplusplus.com/reference/clibrary/cmath/
http://www.cppreference.com/stdmath/index.html

Clifford


Clifford

Author: Joe Gold (joeg)
Posted on:

Rate this post
0 useful
not useful
Thanks for the detailed answer Clifford!

Joe

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.