Why Do Some Machines Require '-lm' for Compiling C Programs While Others Don't?

In summary: This allows the compiler to automatically link to the math library without the need for additional command-line arguments. On machines where it doesn't work, this environment variable may not be set, requiring the '-lm' flag to be explicitly passed. In summary, when writing a program in C that includes mathematical functions from math.h such as sin(), cos(), and pow(), it is important to properly link the math library using the '-lm' flag when compiling with gcc. This flag ensures that any unresolved symbols in the code are linked to the math library, preventing errors during the linking stage. Some compilers may automatically link to the math library, while others may require the use of the '-lm' flag. This can depend on the environment variables set on the
  • #1
Ja4Coltrane
225
0
So I am trying to write a program in C including simple mathematical functions given in math.h. In particular, I want sin(), cos() and pow(). Now when I try to compile (using gcc), I get this error (using powf()):

Undefined first referenced
symbol in file
powf /var/tmp/ccbfbDve.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

Any help?
 
Technology news on Phys.org
  • #2
Could you post your code?
 
  • #3
You are on unix, are you using gcc?
Did you remember to link the maths library with '-lm'
 
  • #4
I am on an ssh with a linux. I don't know what it means to link -lm. My actual code is way too long so I'll send an example which gives the same problem:

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

main ()
{
printf("%f\n", powf(2.0, 3.0));
}
 
  • #5
"ld:" indicates a linking error, not a compilation error. There is nothing wrong with your code, you just aren't linking to the math library. You need to fix your makefile.
 
  • #6
The compiler converts your .c file into a binary .obj file
The linker then converts this into an executable (a.out on linux) including any other system code to do things like maths functions.
GCC calls the linker automatically so you might never see the link command.
For various technical reasons the maths lib is special (eg. you might have different version for speed or precision) and so you have to tell GCC to use it explicity.

Basically just type the same gcc command line as before, but put '-lm' on the end of the line.
 
  • #7
mgb_phys said:
The compiler converts your .c file into a binary .obj file
The linker then converts this into an executable (a.out on linux) including any other system code to do things like maths functions.
GCC calls the linker automatically so you might never see the link command.
For various technical reasons the maths lib is special (eg. you might have different version for speed or precision) and so you have to tell GCC to use it explicity.

Basically just type the same gcc command line as before, but put '-lm' on the end of the line.

Yes, I learned this when I took a C programming class and was very confused when my code compiled fine in windows but would return an error on Solaris. It compiled fine using g++. The CS professor pointed out that you need to use the -lm switch to properly link the math library in gcc.
 
  • #8
vociferous said:
Yes, I learned this when I took a C programming class and was very confused when my code compiled fine in windows but would return an error on Solaris. It compiled fine using g++. The CS professor pointed out that you need to use the -lm switch to properly link the math library in gcc.

Not necessarily true. For instance, a quick straw poll of the five machines in the room I'm sitting in right now (an iBook G4, a MacBook Pro, two Core2 Duo desktops, and a rather menacing Itanium-toting thing, all running either OS X or Debian) shows me that three of them -- the macs and one of the Core2 Duos -- will compile quite happily without passing '-lm' upon linking. All run various versions of gcc 3.x or 4.x.

I've never quite been able to figure out if there's a good reason why some compilers on some machines can automagically handle '-lm' linking, whereas others can't. If anyone can point out why this is so I'd be grateful.
 
Last edited:
  • #9
Code:
gcc mfile.c -o myfile -lm
The -lm part means go to /lib and find a shared library name libm. Link any unresolved symbols against it.
 
  • #10
shoehorn said:
Not necessarily true. For instance, a quick straw poll of the five machines in the room I'm sitting in right now (an iBook G4, a MacBook Pro, two Core2 Duo desktops, and a rather menacing Itanium-toting thing, all running either OS X or Debian) shows me that three of them -- the macs and one of the Core2 Duos -- will compile quite happily without passing '-lm' upon linking. All run various versions of gcc 3.x or 4.x.

I've never quite been able to figure out if there's a good reason why some compilers on some machines can automagically handle '-lm' linking, whereas others can't. If anyone can point out why this is so I'd be grateful.
On the machines that work, there is probably an environment variable (something like CFLAGS or CPPFLAGS) that implicitly passes the '-lm' flag to gcc.
 

1. What are some common errors in C programming?

Some common errors in C programming include syntax errors, logical errors, and runtime errors. Syntax errors occur when the code does not follow the correct syntax or structure of the C language. Logical errors occur when the code does not produce the expected output due to a mistake in the logic or algorithm. Runtime errors occur during the execution of the code and can be caused by issues such as memory leaks or invalid input.

2. How can I improve my debugging skills in C programming?

To improve your debugging skills in C programming, you can start by understanding common errors and their causes. You can also use debugging tools such as debuggers or print statements to track the flow of your code and identify any potential errors. Additionally, practicing debugging on small sections of code can help you become more efficient in finding and fixing errors.

3. What is the difference between "==" and "=" in C programming?

In C programming, "==" is the comparison operator and is used to check if two values are equal. On the other hand, "=" is the assignment operator and is used to assign a value to a variable. It is important to use the correct operator in order to avoid logical errors in your code.

4. How can I optimize my C code for better performance?

To optimize your C code for better performance, you can start by using efficient algorithms and data structures. You can also avoid using unnecessary variables and loops, and instead use built-in functions or preprocessor directives when possible. Additionally, using pointers and memory management techniques can also improve the performance of your code.

5. What are the best practices for writing C code?

Some best practices for writing C code include using meaningful variable and function names, commenting your code to make it more understandable, and breaking your code into smaller, modular functions. It is also important to follow coding conventions and use proper indentation and spacing to make your code more readable. Regularly testing and debugging your code can also help maintain its quality.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
16
Views
5K
  • Programming and Computer Science
Replies
27
Views
4K
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top