Questions about C: Compiler, Math.h, Custom Libraries

  • Thread starter Thread starter The_Doctor
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the use of the C programming language, specifically focusing on the inclusion of the math library via #include <math.h>, the necessity of the -lm option when compiling with gcc, and the creation and usage of custom libraries in C. Participants explore the concepts of linking, headers, and the differences in compiler behavior across platforms.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants note that -lm is required for linking the math library, while others clarify that math.h is a header file that the compiler can find without this option.
  • One participant mentions that the C standard allows for a "minimum subset" version of the language that does not include math functions, which may explain the need for explicit linking in some cases.
  • There is a discussion about the difference between headers and libraries, with one participant explaining that -l is a linker option and that the functions in math.h are found in libm.a.
  • Another participant describes the role of the compiler and linker, noting that they used to be separate programs and that the linker assembles executable programs from compiled parts.
  • Some participants express uncertainty about the specific behavior of Microsoft compilers regarding linking, indicating that they do not require a special linker option.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of linking for certain libraries, but there is disagreement regarding the specifics of how different compilers handle linking and whether -lm is universally required. The discussion remains unresolved on some points, particularly concerning the behavior of Microsoft compilers.

Contextual Notes

There are mentions of differences in compiler behavior across platforms, particularly between gcc and Microsoft compilers, which may affect how libraries are linked. Some participants express confusion about the distinction between headers and libraries, which could lead to misunderstandings in the discussion.

Who May Find This Useful

Readers interested in C programming, particularly those exploring library usage, linking, and compiler behavior across different environments.

The_Doctor
Messages
17
Reaction score
0
Just a few questions about C.

When using #include <math.h>, why is it that when compiling in gcc, I need to add the option -lm. I understand that -l links to some libraries, but why does the compiler need to be linked to whatever 'm' links to? Why can't the compiler find math.h by itself? And what is linking in terms of compilers?

And can I make my own C libraries with my own code in it, like how custom modules can be used in python? And how would I use these custom libraries in other code? Would I just write #include <lib.h> or something? And how do I tell the compiler where to look for these custom libraries?
 
Technology news on Phys.org
I don't remember why, but math.h is the only header in the C standard library that needs to be explicitly linked when compiling.

You can write your own libraries; include them with:
Code:
#include "../path/to/my_library.h"
 
microsoft visual c/c++ uses this syntax to include libraries, although I've only seen it used for the multi-media library.

#pragma comment(lib, "winmm.lib") /* include winmm.lib */

The assembler includes a directive, INCLUDELIB, but that pragma doesn't result in generating an INCLUDELIB if I compile to assembler, so I'm not sure what that pragma actaully does, other than it works (my guess is that it causes the c compiler to pass a parameter to the linker).

old microsoft link about a change to that pragma:

http://support.microsoft.com/kb/153901

current msdn link:

http://msdn.microsoft.com/en-us/library/7f0aews7(v=VS.80).aspx
 
Last edited:
The C standard allows for a "minimum subset" version of the language that does not include the math functions. That was really intended for small microcontrollers that don't support floating point arithmetic in hardware, but the consequence is that you often have to explicitly call up the math library if you want to use it.
 
The_Doctor said:
Just a few questions about C.

When using #include <math.h>, why is it that when compiling in gcc, I need to add the option -lm. I understand that -l links to some libraries, but why does the compiler need to be linked to whatever 'm' links to? Why can't the compiler find math.h by itself? And what is linking in terms of compilers?

And can I make my own C libraries with my own code in it, like how custom modules can be used in python? And how would I use these custom libraries in other code? Would I just write #include <lib.h> or something? And how do I tell the compiler where to look for these custom libraries?

You are getting confused between libraries and headers.
math.h is a header. You don't need -lm for finding math.h
The compiler and find and compile it fine without it.

-l is a linker option. On gcc, -labc tells the linker to find a libabc.a and link to it.
The functions prototyped in math.h exist in a library called libm.a - hence you need to tell the linker to link in a libm.a

This is not just for math.h or libm.a
There are a few libraries which get linked in by default, for all the others you need to specify a -l linker option. As you start writing more programs you will need more & more libraries.

My build scripts had 5-6 -l options a lot of times.Just google for linker and you will find enough info on it.
 
Last edited:
Compiler and linker used to be completely different programs. Compilers are specific to a language like C, Fortran, or whatever, whereas the linker puts together executable programs from the different compiled parts in .o files and .a libraries. The libraries may or may not be specific for a given language.

The reason the linker is called via the C compiler is that it needs to include a few files that are part of each and any C program - the standard library and a bit of code that puts in place argc and argv and then calls main().

Obviously you want your executable to be as small as possible, so you don't include libraries that your program does not need. A lot of basic C programs do not need floating point math, therefore it has been split off into libm.a. Therefore if you do need floating point math, you have to tell the compiler (math.h) and the linker (-lm). The same goes for several other libraries.
 
Thanks to everyone for answering my questions :D
 
Note that microsoft c (at least on msdos or windos) or visual studio doensn't need a special linker option.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
4
Views
5K
Replies
6
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K