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

  • Thread starter The_Doctor
  • Start date
In summary: Just put the libraries in the same directory as your executable and they will be automatically included.
  • #1
The_Doctor
17
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
  • #2
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"
 
  • #3
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:
  • #4
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.
 
  • #5
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:
  • #6
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.
 
  • #7
Thanks to everyone for answering my questions :D
 
  • #8
Note that microsoft c (at least on msdos or windos) or visual studio doensn't need a special linker option.
 

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

1. What is a compiler in C?

A compiler is a program that translates source code written in C into machine code that can be understood and executed by a computer. It takes the code written by a programmer and converts it into a form that the computer's processor can understand.

2. What is the purpose of the "math.h" library in C?

The "math.h" library in C provides a collection of mathematical functions that can be used in a program. These functions include operations such as addition, subtraction, multiplication, division, and more complex mathematical operations such as logarithms, exponentials, and trigonometric functions.

3. How do I create a custom library in C?

To create a custom library in C, you first need to write the code for the functions you want to include in the library. Then, you need to compile the code into an object file using a compiler. Finally, you can create a library file (.lib or .a) using a tool such as "ar" or "lib" and link it to your program during the compilation process.

4. Can I use functions from multiple libraries in the same C program?

Yes, you can use functions from multiple libraries in the same C program. You just need to include the header files for each library and link the corresponding library files during the compilation process.

5. How do I troubleshoot errors when using a custom library in my C program?

If you encounter errors when using a custom library in your C program, you should first check that you have included the correct header file and linked the library file correctly. You can also use debugging tools and check the documentation for the library to troubleshoot any issues. If necessary, you can also reach out to the creator of the library for assistance.

Back
Top