Undefined reference to pow when compiled using gcc [duplicate] - c

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

Related

C: Undefined reference to 'xxx' function [duplicate]

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
'Undefined reference to function' issue even after linking the library which has the defination
(1 answer)
Closed 4 years ago.
I am facing undefined reference issue while creating executable.
Error:
../build/kl/Release/test/maker/guibase.o: In function `decodeAddrList':
tree234.c:(.text+0xc): undefined reference to `ptritclStubsPtr'
tree234.c:(.text+0x20): undefined reference to `ptritclStubsPtr'
tree234.c:(.text+0x12c): undefined reference to `ptritclStubsPtr'
tree234.c:(.text+0x140): undefined reference to `ptritclStubsPtr'
Build Command:-
pkgs/gcc/v4.8.3/bin/gcc -L/usr/X11R6/lib -O2 -DUSE_FLEX -Wall -Wno-char-subscripts -fPIC -DLINUX -DG_DISABLE_CONST_RETURNS -fno-strict-aliasing -o ../build/kl/Release/test/bin/pure.exe -L../build/kl/Release/test/modules ../build/kl/Release/test/maker/constrfunc.TCL.o ../../build/kl/Release/test/maker/pvdbprocs.TCL.o .. ../build/kl/Release/test/maker/maker/memmaker.TCL.o .. ../build/kl/Release/test/maker/modules/libdenbase.a .. ../build/kl/Release/test/maker/guibase.o -lcdnviptcl8.4 -ldenbase -lglib-2.0 -ldenbase -lX11 -ldl -lm -lviputil -lvippli -lcdsCommonMT_sh -lpthread -L/home/dlb/extlibs/arm/lib
I have that symbol defined in the library 'libcdnviptcl8.4' at path '/home/dlb/extlibs/arm/lib' . But still facing the same issue.
Symbol :-
readelf -s libcdnviptcl8.4.so | grep ptritclStubsPtr
636: 00000000000d8068 8 OBJECT GLOBAL DEFAULT 24 itclStubsPtr
1240: 00000000000d8068 8 OBJECT GLOBAL DEFAULT 24 ptritclStubsPtr
Am I missing some thing here? Please help
Quote from comment by OP:
"Due to some privacy issues...I am renaming the symbols.. It was edited by me... It was typo...I just corrected it...:)"
I.e. the identifiers in the error message and the grep line and the output of the grep line have been manually altered.

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

Compile a C program in Linux using shared library [duplicate]

This question already has answers here:
linker tells me it can't resolve symbols, but they're there?
(3 answers)
Closed 7 years ago.
I am trying to compile a simple C program in Linux with a shared library.
I have all together in the same folder the following files:
mymain.c
#include "myclib.h"
int main() {
func();
return 0;
}
myclib.h
void func();
myclib.c
#include <stdio.h>
void func() {
printf("hello world!!!!!!!!!!!!\n");
}
Then I followed these steps:
gcc -c fPIC myclib.c (create memoryaddress independent objectfile)
which produces: myclib.o
gcc -shared -fPIC -o libmyclib.so myclib.o (create shared library)
gcc -c mymain.c (creates an object file out of main.c)
So far so good - then I have the following files ready:
main.o
libmyclib.so
So I try to create a program out of this syntax:
gcc -o program -lmyclib -L. mymain.o
(I guess the prefix lib from libmyclib should be replaced with an l?)
But I get the error message from the gc-compiler:
*mymain.o: In function `main':
mymain.c:(.text+0xa): undefined reference to `func'
collect2: error: ld returned 1 exit status*
I have also tested this syntax:
gcc -o program mymain.c -L -lmyclib -Wl,-rpath,.
Then I get the following error:
/usr/bin/ld: cannot find -lmyclib.so
collect2: error: ld returned 1 exit status
What am I doing wrong in these two implementations?
How do I compile this program to an executable using shared library?
You need to place -l options on the end of linker invocation command line:
gcc -o program -L. mymain.o -lmyclib

undefined reference to `sqrt' [duplicate]

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

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