undefined reference to `sqrt' [duplicate] - c

This question already has answers here:
Undefined reference to sqrt (or other mathematical functions)
(5 answers)
Closed 3 years ago.
Part of my program is to calculate sqrt of float number.
When I write sqrt(1.0f); I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt' - I suppose that in this case the nan value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you

You have to add the -lm flag on most Unix-based systems, as in:
Compile using:
gcc -c file.c
and then link using:
gcc -o program file.o -lm
Or if you don't want to separate the two compilation steps, simply write:
gcc -o program file.c -lm

Link with -lm to link with the math library

Related

Undefined reference to pow when compiled using gcc [duplicate]

This question already has answers here:
Undefined reference to `sin` [duplicate]
(4 answers)
Closed 1 year ago.
I am using pow function in C and included the math.h library too
The following is the error that I am getting :
/usr/bin/ld: /tmp/ccUkOL31.o: in function `main': a1B.c:(.text+0xf3):
undefined reference to 'pow' collect2: error: ld returned 1 exit
status
Although I read on StackOverflow that linking is required. But that's weird. Is there any way to avoid that extra step when we are compiling with gcc. g++ seems to be doing it automatically.
Is there any way to avoid that extra step when we are compiling with gcc?
The way I do it is having a script that invokes gcc (I have ~/bin in my SPATH)
$ cat ~/bin/mycc
#!/bin/sh
gcc -std=c11 -pedantic -Wall $* -lm
$ mycc example1.c example2.c

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.

gcc : undefined reference to 'function' [duplicate]

This question already has answers here:
gcc: undefined reference to
(2 answers)
Closed 5 years ago.
I have installed a C library on my computer (Ubuntu) called xx, whose header file is at /usr/local/include/xx/xx.h and the .a file and .so file are in the path /usr/local/lib named libxx.a and libxx.so.
The test file:
#include<xx/xx.h>
#include<stdio.h>
int main(){
printf("Test\n");
call_function_declared_in_the_header_file();
return 0;
}
When I use gcc to compile gcc test.c -o test, I get the following error:
/tmp/ccb7O0eh.o: In function `main':
test.c:(.text+0xa): undefined reference to `call_function_declared_in_the_header_file'
collect2: error: ld returned 1 exit status
I don't know why. I am not good at the C language. So how can I fix it?
you have to link against the library, i.e. if you want to include <math.h> ( libm.so / libm.a ) you have to link against :
gcc program.c -o program -lm where the -lm invokes the linking
see http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html,
https://www.rapidtables.com/code/linux/gcc/gcc-l.html
compiling always involves the compiler and the linker

-lm doesnt work unless it's at the end of the command [duplicate]

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 2 years ago.
Im currently writing a program for a uni asssessment and they have a set line to compile it, so if it doesn't work with that it won't be accepted.
They command they use is
gcc -Wall -ansi -lm program.c -o program.out
My program will not compile that way, and it'll give me a undefined referance error (Referring to my log10 using math.h library)
if i use:
gcc -Wall -ansi program.c -o program.out -lm
it works
What could be my issue?
Im using windows 10 64bit and have windows bash installed and gcc.
This would be explained if your instructors are using gold and you are using GNU ld. These are two linkers, both are part of the GNU project, and both are commonly used with GCC.
If you are using GNU ld, you get the "traditional" behavior:
The order of specifying the -L and -l options, and the order of specifying -l options with respect to pathname operands is significant.
This means that you have to put -lm after any object files and libraries that depend on it.
However, if you are using gold, the -l options may appear first.
If you have gold installed on your system, you can test it yourself.
Here is what I get:
$ gcc -lm program.c
/tmp/ccJmBjmd.o: In function `main':
program.c:(.text+0x15): undefined reference to `sin'
collect2: error: ld returned 1 exit status
But if I use gold, it works fine:
$ gcc -lm program.c -fuse-ld=gold
-lm needs to be at the end of the command, most likely in the first case with the literal the compiler is optimizing out the call to any function and therefore does not need to link against the library. This is called constant folding and for example we can see in the gcc docs on Other Built-in Functions Provided by GCC says:
GCC includes built-in versions of many of the functions in the
standard C library. The versions prefixed with __builtin_ are always
treated as having the same meaning as the C library function even if
you specify the -fno-builtin option. (see C Dialect Options) Many of
these functions are only optimized in certain cases; if they are not
optimized in a particular case, a call to the library function is
emitted.

sqrt() function not working with variable arguments [duplicate]

This question already has answers here:
sqrt from math.h causes linker error "undefined reference to sqrt" only when the argument is not a constant
(1 answer)
Why am I getting "undefined reference to sqrt" error even though I include math.h header? [duplicate]
(5 answers)
Closed 4 years ago.
I don't know if I'm missing something obvious, but it appears that I'm unable to compute square roots of a variable in C; the sqrt() function only seems to work on constants. This is my code:
#include <math.h>
#include <stdio.h>
int main()
{
double a = 2.0;
double b = sqrt(a);
printf("%f", b);
return 0;
}
When I run this program, I get the following error:
gcc -Wall -o "test2" "test2.c" (in directory: /home/eddy/Code/euler)
/tmp/ccVfxkNh.o: In function `main':
test2.c:(.text+0x30): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Compilation failed.
However, if I replace the argument in sqrt() with a constant such as 2.0 for example, (b = sqrt(2.0)), then it works fine. Is sqrt() not supposed to work with variables or something?
Thanks for the help
You need to link with the math library (use a '-lm' on the command line). In the constant case, the compiler is probably being smart and precomputing sqrt(2.0) (so the code that is compiled is essentially 'b = 1.414...;')
In case of gcc you need to link the library.
gcc filename.c -lm .
However in case of g++ no need to link the library so this will work fine :
g++ filename.c -o filename
Once compilation is successful.
To run simply enter ./filename in G++.
and enter ./a.out in Gcc.
Use the command gcc -Wall -o "test2" "test2.c" -lm which will likely fix this.
This includes the math library in addition to the standard C runtime library. On most systems, the math library is historically a separate entity that needs to be explicitly requested.
Compile with:
gcc -Wall -o test2 test2.c -lm
You need to link against the math library.
include math library using " " operator
#include " math.h "
compile program using -lm option for inherit math library
suppose our program name is test.c the we compile as follow
gcc test.c -lm
gcc does not link the standard libraries by default. So you just need to do this if compiling via gcc:
gcc filename.c -lm .
However in case of g++ no need to link the library so this will work fine :
g++ filename.c -o filename
This works fine for me. I think there is some problem with ur math library. Try linking it again and see. Other wise code is completely perfect.

Resources