math.h - Cannot compile Code with log() and rand() function [duplicate] - c

This question already has answers here:
Undefined reference to `sin` [duplicate]
(4 answers)
gcc will not properly include math.h
(2 answers)
Closed 8 years ago.
When i compile the following code snippet I get this error message from my compiler:
/tmp/ccT1yBa1.o: In function `main':
test.c:(.text.startup+0x34): undefined reference to `log'
collect2: error: ld returned 1 exit status
_
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>
void main(){
srand(time(NULL));
double r1 = (rand() % 1000)/1000.0;
double r2 = log(r1);
printf("%lf\n",r2);
}
Compiled with
gcc -O2 test.c
What's wrong?

You have forgot the -lm to link with the math library.
gcc -O2 test.c -lm

Related

I am trying to implement a code using 'extern' keyword, IDE: VS Code (using code runner...) [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 months ago.
extrn.c
#include <stdio.h>
extern int var;
int main()
{
printf("%d", var);
return 0;
}
var.c
int var = 5;
I go to file extrn.c and I run the code and I get this:
undefined reference to `var'
and this is what my output is looking like:
[Running] cd "/home/buff/Documents/Coding/C/C_programming_NESO/" && gcc extrn.c -o extrn && "/home/buff/Documents/Coding/C/C_programming_NESO/"
/usr/bin/ld: /tmp/ccoKgi02.o: in function `main':
extrn.c:(.text+0xa): undefined reference to `var'
collect2: error: ld returned 1 exit status
[Done] exited with code=1 in 0.093 seconds
Compile your both C files together to fix this undefined reference error.
For GCC
gcc extrn.c var.c -o main
For clang
clang extrn.c var.c -o main

GCC error undefined reference to `sqrt' on changing command option order [duplicate]

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Why does the order of '-l' option in gcc matter? [duplicate]
(3 answers)
Closed 1 year ago.
Why I am getting the following error
/tmp/ccuWdVB3.o: In function `test':
MyCode.c:(.text+0x1c): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
MyCode.c
#include<stdio.h>
#include<math.h>
int test(int input1)
{
int x = 8;
int z = sqrt(x);
}
int main(int argc, char * a[]) {
}
when running with command:
gcc -o a.out -lm MyCode.c
But when I am running the command
gcc MyCode.c -o a.out -lm
everything works fine. Why the order of "MyCode.c" cli option is important here ?
Or Am i doing something wrong?
Libraries are searched only once during the linking (they may contain millions of symbols and objects) and that is the reason why they should be specified as the last ones when all objects linker has to search for are already known. Searching the giant library for the symbols after every object file in the project would be extremely inefficient.

Program using ceil() in C [duplicate]

This question already has answers here:
C Failing to compile: Can't find math.h functions [duplicate]
(2 answers)
Closed 3 years ago.
I did the following code using ceil()
#include<stdio.h>
#include<math.h>
int main()
{
float val;
float cVal;
val=23.4;
cVal =ceil(val);
printf("ceil value:%f\n",cVal);
return 0;
}
I am getting th following error
In function main':
test1.c:(.text+0x1b): undefined reference toceil'
collect2: error: ld returned 1 exit status
What is wrong in this code??Please help!
I compiled it using makefile
>>cmake .
>>make
>>./hello.out
From https://askubuntu.com/a/745199/513302
If you are going to compile a C program with math.h library in Linux using gcc you will have to use –lm option in the compiler command-line
gcc xyz.c -o xyz -lm
The -lm option is actually -l (for "link a module") and m is shorthand for the built-in math library.

Undefined reference to 'function' yet the header is included [duplicate]

This question already has answers here:
C program cannot find function which included in header file
(2 answers)
Closed 7 years ago.
I am playing around with the ci20 and flowcloud. I have downloaded their c library/sdk and have included the header.
The program is simply:
#include <stdio.h>
#include <stdbool.h>
#include <flow/flowcore.h>
int main (int argc, char*argv[])
{
printf("hello");
if(FlowCore_Initialise()) printf("init");
return (0);
}
but on compilation gcc -Wall test.c -o hello I get this error:
/tmp/cch4kocL.o: In function `main':
test.c:(.text+0x40): undefined reference to `FlowCore_Initialise'
collect2: error: ld returned 1 exit status
I am not 100% sure what is going on here.
You are getting a linker error. When compiling with gcc, you need to also specifiy the library you want to link to in addition to just using the proper #include 's.
The syntax for compiling with gcc is
$ gcc [options] [source files] [object files] [-Ldir] -llibname [-o outfile]
As you can see you can link the library by adding -l<name> to your command string.

Why am I getting this error when trying to use log from math.h in C? [duplicate]

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 9 years ago.
I need to use logs in a program for an assignment. I ran this test program on my machine to see how the log function works (and if it would), and I get the following error during build.
Code
/* log example */
#include <stdio.h> /* printf */
#include <math.h> /* log */
int main()
{
double param, result;
param = 5.5;
result = log (param);
printf ("log(%f) = %f\n", param, result );
return 0;
}
ERROR
gcc -Wall -o "test" "test.c" (in directory: /home/linux/Development/codetest)
/tmp/ccCDqX7x.o: In function `main':
test.c:(.text+0x1b): undefined reference to `log'
collect2: ld returned 1 exit status
Compilation failed.
Link
This is C99 code, grabbed from this tutorial site.
Add -lm to your compilation command to link in the math library.
gcc -Wall -o "test" "test.c" -lm

Resources