undefined reference to `sqrt', 'log', [duplicate] - c

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 3 years ago.
When I compile this code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main() {
char str[] = "3.6";
double res = atof(str);
printf("%f\n", sqrt(res));
return 0;
}
I get this error:
====================[ Build | untitled12 | Debug ]==============================
/snap/clion/81/bin/cmake/linux/bin/cmake --build /home/abdo/CLionProjects/untitled12/cmake-build-debug --target untitled12 -- -j 4
[ 50%] Linking C executable untitled12
/usr/bin/ld: CMakeFiles/untitled12.dir/main.c.o: in function `main':
/home/abdo/CLionProjects/untitled12/main.c:11: undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/untitled12.dir/build.make:84: untitled12] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/untitled12.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/untitled12.dir/rule] Error 2
make: *** [Makefile:118: untitled12] Error 2
I get same error when I replace 'sqrt' with 'log' or 'ln' ...
compiler: cc (Ubuntu 8.3.0-6ubuntu1) 8.3.0

The math library must be linked in when compiling. Add the -lm flag to your gcc command, after the source or object file(s).

Related

1 I am doing the cs50 course and decided to run the code from there on my MacBook [duplicate]

This question already has an answer here:
CS50 get_string error linker command failed with exit code 1
(1 answer)
Closed 13 days ago.
I have the following code:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int x = get_int("whats x? ");
int y = get_int("whats y? ");
if (x < y)
{
printf("x is less than y\n");
}
}
It keeps giving me this error
moutarrjallow#Moutarrs-MBP cclang % make ccompare
cc ccompare.c -o ccompare
Undefined symbols for architecture x86_64:
"_get_int", referenced from:
_main in ccompare-91f0e5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [ccompare] Error 1
You need to link with the cs50 library by adding the -lcs50 to above. In your Makefile you can probably do that by setting LDLIBS := -lcs50.

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

How to add Compiler Flag in CMakeLists Clion for shm_open [duplicate]

This question already has answers here:
undefined reference to `shm_open' using CMake
(2 answers)
Closed 1 year ago.
Simple Program:
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
int main() {
int shm_fd;
shm_fd=shm_open("sh",O_CREAT|O_RDWR,0666);
return 0;
}
The error is:
Warnung: undefined reference to »shm_open«
collect2: error: ld returned 1 exit status
CMakeFiles/Share_Memory_Project.dir/build.make:102: recipe for target 'Share_Memory_Project' failed
make[3]: *** [Share_Memory_Project] Error 1
CMakeFiles/Makefile2:94: recipe for target 'CMakeFiles/Share_Memory_Project.dir/all' failed
make[2]: *** [CMakeFiles/Share_Memory_Project.dir/all] Error 2
CMakeFiles/Makefile2:101: recipe for target 'CMakeFiles/Share_Memory_Project.dir/rule' failed
make[1]: *** [CMakeFiles/Share_Memory_Project.dir/rule] Error 2
Makefile:137: recipe for target 'Share_Memory_Project' failed
make: *** [Share_Memory_Project] Error 2
To fix this Problem, I have to add a Compiler Flag:
-lrt at the end. Like this: gcc main.c -o main -lrt. So When I put this in Command Line at Ubuntu Terminal it compile fine.
My Question is: How to add this Flag at my CMakeLists.txt in Clion?
How my file currently looks:
cmake_minimum_required(VERSION 3.19)
project(Share_Memory_Project C)
set(CMAKE_C_STANDARD 99)
add_executable(Share_Memory_Project main.c)
To link a target with a library, use target_link_libraries. You would:
add_executable(Share_Memory_Project main.c)
target_link_libraries(Share_Memory_Project PUBLIC rt)

Math library linking error in CLion on Linux [duplicate]

This question already has answers here:
How to link to the C math library with CMake?
(3 answers)
Closed 3 years ago.
To do my homework I need #include "math.h", but after updating GCC and CMake, CLion can't link my project files. What should I do to fix this problem?
In Settings -> Build, Execution and Deployment -> Toolchains CLion says that CMake version is 3.15.3 and GDB version is 8.3 and it's OK.
I already tired to reinstall GCC, CMake and CLion, but it didn't work. Also I tired to search some info on StackOverflow, but still nothing works.
Main.c:
#include <stdio.h>
#include <math.h>
int main() {
FILE *output;
output = fopen("/home/vadimsam/CLionProjects/untitled/data.txt", "w");
double x=0.,v=0.,t=0.,m=0.,k=0.,dt = 1e-5,xn,vn;
while (t < 1e1) {
vn = -x*sqrt((k/m))*cos(sqrt((k/m))*t)+v*cos(sqrt((k/m))*t);
xn = -x*cos(sqrt((k/m))*t)+(v/sqrt((k/m)))*sin(sqrt((k/m))*t);
t += dt; x = xn; v = vn;
fprintf(output, "%lf %lf %lf\n", t, x, v);
}
fclose(output);
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(untitled2 C)
set(CMAKE_C_STANDARD 11)
add_executable(untitled2 main.c)
Compiler output:
====================[ Build | untitled2 | Debug ]===============================
/home/vadimsam/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/193.5096.27/bin/cmake/linux/bin/cmake --build /home/vadimsam/CLionProjects/untitled2/cmake-build-debug --target untitled2 -- -j 8
Scanning dependencies of target untitled2
[ 50%] Building C object CMakeFiles/untitled2.dir/main.c.o
[100%] Linking C executable untitled2
CMakeFiles/untitled2.dir/main.c.o: In function `main':
/home/vadimsam/CLionProjects/untitled2/main.c:10: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:10: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:10: undefined reference to `cos'
/home/vadimsam/CLionProjects/untitled2/main.c:10: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:10: undefined reference to `cos'
/home/vadimsam/CLionProjects/untitled2/main.c:11: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:11: undefined reference to `cos'
/home/vadimsam/CLionProjects/untitled2/main.c:11: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:11: undefined reference to `sqrt'
/home/vadimsam/CLionProjects/untitled2/main.c:11: undefined reference to `sin'
collect2: error: ld returned 1 exit status
CMakeFiles/untitled2.dir/build.make:83: recipe for target 'untitled2' failed
make[3]: *** [untitled2] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/untitled2.dir/all' failed
make[2]: *** [CMakeFiles/untitled2.dir/all] Error 2
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/untitled2.dir/rule' failed
make[1]: *** [CMakeFiles/untitled2.dir/rule] Error 2
Makefile:118: recipe for target 'untitled2' failed
make: *** [untitled2] Error 2
I need to compile my project.
The math library is usually linked as a separate library (conveniently named m) that you explicitly need to link with.
You tell CLion (through its CMakeLists.txt file) to link with libraries with the target_link_libraries command:
target_link_libraries(untitled2 m)

Undefined reference to "graphics.h" functions in Ubuntu 19.04 in CLion IDE

I installed graphics.h library on my Ubuntu 19.04, and when I write some code in my CLion IDE, it successfully includes the graphics.h Header file, and also after writing some basic functions, the IDE doesn't show any errors, but when I compile the Code it gives Errors.
My Code:
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
detectgraph(&gd, &gm);
initgraph(&gd, &gm, NULL);
line(10, 10, 50, 50);
closegraph();
}
My CMakeList.txt File:
cmake_minimum_required(VERSION 3.14)
project(CGR_MP C)
set(CMAKE_C_STANDARD 11)
include_directories("//usr//local//lib//libgraph-1.0.2//")
build_command("lgraph")
add_executable(CGR_MP main.c)
Errors:
====================[ Build | CGR_MP | Debug ]==================================
/snap/clion/73/bin/cmake/linux/bin/cmake --build /home/prathamesh/Desktop/College/Sem-2/CGR-MP/cmake-build-debug --target CGR_MP -- -j 2
Scanning dependencies of target CGR_MP
[ 50%] Building C object CMakeFiles/CGR_MP.dir/main.c.o
[100%] Linking C executable CGR_MP
/usr/bin/ld: CMakeFiles/CGR_MP.dir/main.c.o: in function `main':
/home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:7: undefined reference to `detectgraph'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:8: undefined reference to `initgraph'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:10: undefined reference to `line'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:12: undefined reference to `closegraph'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/CGR_MP.dir/build.make:84: CGR_MP] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/CGR_MP.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/CGR_MP.dir/rule] Error 2
make: *** [Makefile:118: CGR_MP] Error 2
Despite everything being successfully installed, it shows all Graphics functions as undefined reference

Resources