C libm.a not needed to be linked when compiling - c

I was trying to compile a source file that includes <math.h>.
However I succeeded in creating an executable, no error without linking to libm.a.
The command I typed was gcc -Wall filename.c -o executablename
I was told to link to the external libraries (i.e/ libraries other than libc.a)
What's going on?
#include <math.h>
#include <stdio.h>
int main(void)
{
double x = sqrt(2.0);
printf ("The sqrt of 2 is: %f\n", x);
return 0;
}

The math functions you call are implemented by compiler built-in functions. Try the following if you want to see an error message:
gcc -fno-builtin -Wall filename.c -o executablename
For example, on my platform (Ubuntu 14.04.3 LTS), I get this error message:
$ cat x.c
#include <math.h>
#include <stdio.h>
int main(void)
{
double x = sqrt(2.0);
printf ("The sqrt of 2 is: %f\n", x);
return 0;
}
$ gcc -fno-builtin x.c
/tmp/ccpjG2Pb.o: In function `main':
x.c:(.text+0x1c): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

Some compilers, like the current clang on OS X (which masquerades as gcc) do not need to be told to link your executable with the math library.
The clang on OS X will link your executable with /usr/lib/libSystem.B.dylib (and only this for a simple program). This library in turn uses the library /usr/lib/system/libsystem_m.dylib which is the math library.

The functions in <math.h> (or the preferable <tgmath.h>) are part of the C library just as many other functions. It is platform dependent if all the functions in the C library are actually linked in one single library or separately in multiple chunks.
In ancient times the size of the libraries was a problem for link time, the larger a library was, the longer it took to link executables that had many unresolved symbols. These times are long gone, but the separation on some platforms into libc.a and libm.a prevails.
If your platform doesn't need -lm for linking it should have a dummy (empty) version of libm.a just that there is no error when you have that on your link command line. This is for example the case for the musl C library that is at the base of Alpine Linux.

Related

Error "undefined reference to symbol 'sqrt##GLIBC_2.17'" [duplicate]

I have this simple code:
max = (int) sqrt (number);
and in the header I have:
#include <math.h>
But application still says undefined reference to sqrt. Do you see any problem here? It looks like everything should be okay.
You may find that you have to link with the math libraries on whatever system you're using, something like:
gcc -o myprog myprog.c -L/path/to/libs -lm
^^^ - this bit here.
Including headers lets a compiler know about function declarations but it does not necessarily automatically link to the code required to perform that function.
Failing that, you'll need to show us your code, your compile command and the platform you're running on (operating system, compiler, etc).
The following code compiles and links fine:
#include <math.h>
int main (void) {
int max = sqrt (9);
return 0;
}
Just be aware that some compilation systems depend on the order in which libraries are given on the command line. By that, I mean they may process the libraries in sequence and only use them to satisfy unresolved symbols at that point in the sequence.
So, for example, given the commands:
gcc -o plugh plugh.o -lxyzzy
gcc -o plugh -lxyzzy plugh.o
and plugh.o requires something from the xyzzy library, the second may not work as you expect. At the point where you list the library, there are no unresolved symbols to satisfy.
And when the unresolved symbols from plugh.o do appear, it's too late.
I suppose you have imported math.h with #include <math.h>
So the only other reason I can see is a missing linking information. You must link your code with the -lm option.
If you're simply trying to compile one file with gcc, just add -lm to your command line, otherwise, give some informations about your building process.
Just adding the #include <math.h> in c source file and -lm in Makefile at the end will work for me.
gcc -pthread -o p3 p3.c -lm
Here are my observation, firstly you need to include the header math.h as sqrt() function declared in math.h header file. For e.g
#include <math.h>
secondly, if you read manual page of sqrt you will notice this line Link with -lm.
#include <math.h> /* header file you need to include */
double sqrt(double x); /* prototype of sqrt() function */
Link with -lm. /* Library linking instruction */
But application still says undefined reference to sqrt. Do you see any
problem here?
Compiler error is correct as you haven't linked your program with library lm & linker is unable to find reference of sqrt(), you need to link it explicitly. For e.g
gcc -Wall -Wextra -Werror -pedantic test.c -lm
I had the same issue, but I simply solved it by adding -lm after the command that runs my code.
Example.
gcc code.c -lm

Bash script error "undefined reference to sin" [duplicate]

I have this simple code:
max = (int) sqrt (number);
and in the header I have:
#include <math.h>
But application still says undefined reference to sqrt. Do you see any problem here? It looks like everything should be okay.
You may find that you have to link with the math libraries on whatever system you're using, something like:
gcc -o myprog myprog.c -L/path/to/libs -lm
^^^ - this bit here.
Including headers lets a compiler know about function declarations but it does not necessarily automatically link to the code required to perform that function.
Failing that, you'll need to show us your code, your compile command and the platform you're running on (operating system, compiler, etc).
The following code compiles and links fine:
#include <math.h>
int main (void) {
int max = sqrt (9);
return 0;
}
Just be aware that some compilation systems depend on the order in which libraries are given on the command line. By that, I mean they may process the libraries in sequence and only use them to satisfy unresolved symbols at that point in the sequence.
So, for example, given the commands:
gcc -o plugh plugh.o -lxyzzy
gcc -o plugh -lxyzzy plugh.o
and plugh.o requires something from the xyzzy library, the second may not work as you expect. At the point where you list the library, there are no unresolved symbols to satisfy.
And when the unresolved symbols from plugh.o do appear, it's too late.
I suppose you have imported math.h with #include <math.h>
So the only other reason I can see is a missing linking information. You must link your code with the -lm option.
If you're simply trying to compile one file with gcc, just add -lm to your command line, otherwise, give some informations about your building process.
Just adding the #include <math.h> in c source file and -lm in Makefile at the end will work for me.
gcc -pthread -o p3 p3.c -lm
Here are my observation, firstly you need to include the header math.h as sqrt() function declared in math.h header file. For e.g
#include <math.h>
secondly, if you read manual page of sqrt you will notice this line Link with -lm.
#include <math.h> /* header file you need to include */
double sqrt(double x); /* prototype of sqrt() function */
Link with -lm. /* Library linking instruction */
But application still says undefined reference to sqrt. Do you see any
problem here?
Compiler error is correct as you haven't linked your program with library lm & linker is unable to find reference of sqrt(), you need to link it explicitly. For e.g
gcc -Wall -Wextra -Werror -pedantic test.c -lm
I had the same issue, but I simply solved it by adding -lm after the command that runs my code.
Example.
gcc code.c -lm

ceil() only works for rvalues [duplicate]

This question already has answers here:
Why does the order of '-l' option in gcc matter? [duplicate]
(3 answers)
Closed 3 years ago.
I'm trying to compile this program in ubuntu 18.04, 64 bits:
#include <math.h>
#include <stdio.h>
int main() {
double x = 1.9;
float y = 1.8;
int x2 = ceil(x);
int y2 = ceil(y);
printf("%d, %d\n", x2, y2);
return 0;
}
The gcc command I'm using is:
gcc -std=c99 -lm main.c -o main
And the error I'm obtaining is:
/tmp/ccWL94J9.o: In function `main':
main.c:(.text+0x30): undefined reference to `ceil'
main.c:(.text+0x41): undefined reference to `ceil'
collect2: error: ld returned 1 exit status
Although, if I replace ceil(x) by ceil(1.2) for example, and something similar for ceil(y), I can build and execute the program.
In addition, I have checked that I do have libm.so installed:
bash> find /usr/lib -name "*libm.so*"
/usr/lib/x86_64-linux-gnu/libm.so
What I'm missing?
With the following line, it compiles:
gcc -std=c99 main.c -o main -lm
(putting -lm after -o main)
See https://stackoverflow.com/a/11894098/4030665
With some expressions E, particularly constants, the compiler can evaluate ceil(E) while it is compiling, and it does so if this optimization is not disabled. Then the compiler generates code that uses the result and does not call ceil. When the compiler fails to evaluate ceil(E) during compilation, it generates code that calls ceil.
The switch -lm is an abbreviation for the standard math library. The linker processes input files in the order they appear on the command line. When the linker processes a library, it extracts from the library all object modules that contain a definition for a symbol that is currently needed (referenced but not defined) in the executable file (or other output) it is building.
GCC maintains the order of the various units on its command line. Given -lm main.c, it compiles main.c to product an object module, and then it passes -lm and the object module to the linker in that order. Since the math library is first, when the linker processes it, it has not yet seen any symbols that reference it, and therefore it does not take any modules from the library.
If GCC is instead given main.c -lm, the linker processes the math library after the object module for main. Then, when the linker is processing the math library, it will know that main references ceil, so it will extract from the math library the module that defines ceil.
Thus gcc -std=c99 -lm main.c -o main will work with source code that uses only constants with ceil but will not work with the source code in the example, but gcc -std=c99 main.c -lm -o main will work.

I used pow() function in my C program but did not link to -lm it still worked. Why?

I was reading a book on GCC. It said only the standard library is linked by default for any C program. Since the pow() is not in the standard library, I will have to link to it using the -lm flag. However, when I compiled, I simply used:
gcc hello.c -o hello
and it still worked..
And there is another similar problem, the book also said that if you have printf("%f\n", 4); in your C program and if you compile WITHOUT -Wall option, no warning will be issued. However, I tried compiling it without the -Wall option but I still got a warning:
hello.c:6:2: warning: format ‘%f’ expects argument of type ‘double’,
but argument 2 has type ‘int’ [-Wformat]
Why is this? The book said I have to supply -lm and -Wall in order to make my program compiled and get the warning but I did not use either of them but I still got my program compiled and got the warning?
Thank you!
A bit more information based on some experimentation.
Consider this program:
#include <stdio.h>
#include <math.h>
int main(void) {
#ifdef CONSTANT
double x = pow(2.0, 10.0);
#else
double expon = 10.0;
double x = pow(2.0, expon);
#endif
printf("x = %f\n", x);
return 0;
}
On Ubuntu, when I compile it with
$ gcc c.c -o c
it complains about an undefined reference to pow; adding -lm corrects it.
But this:
$ gcc -DCONSTANT c.c -o c
compiles and links without error, replacing the pow() call with a constant 1024.0. But this:
$ gcc -fno-builtin -DCONSTANT c.c -o c
complains again about the undefined reference to pow.
Conclusion: gcc uses the built-in implementation of pow only when the result can be determined at compile time. Otherwise it generates an explicit call to the pow() function, which requires linking with -lm.
Note also that this depends on the way the C library is organized (the library is provided separately; it's not part of gcc). On Cygwin, which uses newlib rather than the glibc used on Ubuntu, the -lm option is not needed; apparently the math routines are an integral part of the standard library rather than being provided separately. (gcc on Cygwin still accepts the -lm option.)
GCC supplies several standard library functions as built-ins:
GCC provides a large number of built-in functions other than the ones mentioned above. Some of these are for internal use in the processing of exceptions or variable-length argument lists and will not be documented here because they may change from time to time; we do not recommend general use of these functions.
The remaining functions are provided for optimization purposes.
If you look at the list of built-ins, you'll see that pow is one of them.
If you add -fno-builtin to your compiler options, you should get the linker error that you're expecting.

Undefined reference to sqrt (or other mathematical functions)

I have this simple code:
max = (int) sqrt (number);
and in the header I have:
#include <math.h>
But application still says undefined reference to sqrt. Do you see any problem here? It looks like everything should be okay.
You may find that you have to link with the math libraries on whatever system you're using, something like:
gcc -o myprog myprog.c -L/path/to/libs -lm
^^^ - this bit here.
Including headers lets a compiler know about function declarations but it does not necessarily automatically link to the code required to perform that function.
Failing that, you'll need to show us your code, your compile command and the platform you're running on (operating system, compiler, etc).
The following code compiles and links fine:
#include <math.h>
int main (void) {
int max = sqrt (9);
return 0;
}
Just be aware that some compilation systems depend on the order in which libraries are given on the command line. By that, I mean they may process the libraries in sequence and only use them to satisfy unresolved symbols at that point in the sequence.
So, for example, given the commands:
gcc -o plugh plugh.o -lxyzzy
gcc -o plugh -lxyzzy plugh.o
and plugh.o requires something from the xyzzy library, the second may not work as you expect. At the point where you list the library, there are no unresolved symbols to satisfy.
And when the unresolved symbols from plugh.o do appear, it's too late.
I suppose you have imported math.h with #include <math.h>
So the only other reason I can see is a missing linking information. You must link your code with the -lm option.
If you're simply trying to compile one file with gcc, just add -lm to your command line, otherwise, give some informations about your building process.
Just adding the #include <math.h> in c source file and -lm in Makefile at the end will work for me.
gcc -pthread -o p3 p3.c -lm
Here are my observation, firstly you need to include the header math.h as sqrt() function declared in math.h header file. For e.g
#include <math.h>
secondly, if you read manual page of sqrt you will notice this line Link with -lm.
#include <math.h> /* header file you need to include */
double sqrt(double x); /* prototype of sqrt() function */
Link with -lm. /* Library linking instruction */
But application still says undefined reference to sqrt. Do you see any
problem here?
Compiler error is correct as you haven't linked your program with library lm & linker is unable to find reference of sqrt(), you need to link it explicitly. For e.g
gcc -Wall -Wextra -Werror -pedantic test.c -lm
I had the same issue, but I simply solved it by adding -lm after the command that runs my code.
Example.
gcc code.c -lm

Resources