C program compilation error: undefined reference - c

I am unable to compile the following simple C code and I don't know why.
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <math.h>
int main(){
double result;
result = cos(0.5);
printf("asin(0.5) is %f\n", result);
return 0;
}
The error message I receive after I try to compile is -
In function
'main':
test.c:(.text+0xlc): undefined reference to 'cos'
collect2: ld
returned 1 exit status

You need to link with the math library (-lm).
gcc -Wall -Wextra -o test test.c -lm
See this C FAQ.

In general whenever you get undefined reference error it's due to the compiler is not able to find your function definition. So it may be your function ( and you have not typed the spelling of function correctly so you will get this error ) or may be built-in function like you have encountered in this case.
to explore there are various library and their linking are necessary at the time of compilation
whenever you use math function use -lm ( l stands for link and m is for math )
in pthread built-in functions use -lpthread
and so on ...
In this case indeed use -lm
gcc -lm test.c
will be able to compile your program .

Related

math.h's fmod function giving error when compiling in "C" [duplicate]

Sample code for fmod:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 0.14527, y = 3.14159;
printf("fmod(x, y) = %.6lf\n", fmod(x, y));
return 0;
}
Compiling:
$ gcc main.c -o main
I get
/tmp/ccztJO01.o: In function `main':
main.c:(.text+0x4d): undefined reference to `fmod'
collect2: ld returned 1 exit status
Then I found this in Google:
$ gcc -lm main.c -o main
Why should I use -lm, what is it exactly? From where I can get more information about gcc in detail?
-lm is simply telling it to link libm, which contains all the floating point math routines, including (no surprise here) fmod.
When I input gcc -lm main.c -o main I still get a linker error. I need to write gcc main.c -lm -o main for it work right. If it's working for you the other way, that's a bit odd. I understand that the linker will find the symbol declared in main.c (i.e. double fmod(double,double)), but only resolve it if it finds its definition later on (i.e. in libm.a).
Long story short, the libraries must be placed (at least once) "to the right of" the place where they are used.
It's not the compiler, but the linker, ld, that is complaining. It cannot find the routine fmod in your program. You have to tell it to link with math library libm with the -l flag.
[Much] more info: GCC, the GNU Compiler Collection.

undefined reference to `UNITY_BEGIN' and 'UNITY_END'

I want to use Unity for making unit test on my C code but I get the following error when compiling:
$ make
gcc ../Unity/src/unity.o src/helpers.o src/chess.o src/search.o test/TestCheck.o src/main.o -o checkai
src/main.o: In function `main':
main.c:(.text+0x4b): undefined reference to `UNITY_BEGIN'
main.c:(.text+0x55): undefined reference to `UNITY_END'
collect2: error: ld returned 1 exit status
make: *** [Makefile:11: checkai] Error 1
$ make -n
gcc ../Unity/src/unity.o src/helpers.o src/chess.o src/search.o test/TestCheck.o src/main.o -o checkai
My main functon just looks like this:
#include <stdio.h>
#include <stdlib.h>
#include "helpers.h"
#include "chess.h"
#include "../../Unity/src/unity.h"
//#include "../test/TestCheck.h"
void test_Smoke(void) {
TEST_ASSERT_EQUAL_INT(1, 2);
}
int main() {
version();
chesspiece chessboard[8][8];
initField(chessboard);
UNITY_BEGIN();
RUN_TEST(test_Smoke, 1);
return UNITY_END();
}
Do I link something incorrectly? I already tried to change the order of the gcc command but nothing has helped yet.
When I comment out UNITY_BEGIN and UNITY_END I can compile it and the smoke test runs without problems.
I could solve it by not using the release version 2.1.0.
To solve it I have cloned the current master branch and it just worked even with UNITY_BEGIN and UNITY_END:
git clone https://github.com/ThrowTheSwitch/Unity.git

undefined reference to `roundf' - C language

I'm new to C and I keep running into the same error. I am typing the code in nano text editor and compiling it in the linux terminal. My operating system is Ubuntu 14.04. Here's an example of code that won't compile,
#include <math.h>
#include <stdio.h>
int main()
{
float x = 23.33f;
x = roundf(x);
printf("%f\n", x);
return (0);
}
The error I am receiving is,
cc example.c -o example
/tmp/ccWV0Or8.o: In function `main':
example.c:(.text+0x1d): undefined reference to `roundf'
collect2: error: ld returned 1 exit status
make: *** [example] Error 1
Any help is much appreciated, thanks!
Link with the math library:
cc example.c -o example -lm
The math library functions are not part of standard C library which is linked by default. So you need link it yourself.
There's an interesting thread about why it's not part of libc:
Why do you have to link the math library in C?

Calling a C function defined in another file

I know this question has a lot of answers, but I am always getting an error on gcc c1.c
c1.c:(.text+0x5): undefined reference to `f'
collect2: error: ld returned 1 exit status
no matter what I try.
This is c1.c
#include <stdio.h>
#include "c.h"
int main()
{
printf("F %d\n",f());
}
THis is c2.c
#include <stdio.h>
int f(void) {return 7;}
int main()
{
printf("S %d\n",f());
}
This is c.h
int f(void);
How could I get it working? Actually, I was getting this error in a big program that is modelled like this. I guess this is the way to do it.
(And yes, main should return 0).
You're going to have problems, since you have two different main functions defined, but the general way to do this is a link-time issue:
gcc -c c1.c
gcc -c c2.c
gcc c1.o c2.o
All you need to do is figure out which main you want, and remove the unwanted one.
You could do something like:
gcc -Dmain=blah -c c1.c
gcc -c c2.c
gcc c1.o c2.o

gcc gives error while using fmod()

Sample code for fmod:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 0.14527, y = 3.14159;
printf("fmod(x, y) = %.6lf\n", fmod(x, y));
return 0;
}
Compiling:
$ gcc main.c -o main
I get
/tmp/ccztJO01.o: In function `main':
main.c:(.text+0x4d): undefined reference to `fmod'
collect2: ld returned 1 exit status
Then I found this in Google:
$ gcc -lm main.c -o main
Why should I use -lm, what is it exactly? From where I can get more information about gcc in detail?
-lm is simply telling it to link libm, which contains all the floating point math routines, including (no surprise here) fmod.
When I input gcc -lm main.c -o main I still get a linker error. I need to write gcc main.c -lm -o main for it work right. If it's working for you the other way, that's a bit odd. I understand that the linker will find the symbol declared in main.c (i.e. double fmod(double,double)), but only resolve it if it finds its definition later on (i.e. in libm.a).
Long story short, the libraries must be placed (at least once) "to the right of" the place where they are used.
It's not the compiler, but the linker, ld, that is complaining. It cannot find the routine fmod in your program. You have to tell it to link with math library libm with the -l flag.
[Much] more info: GCC, the GNU Compiler Collection.

Resources