I've got some C code which is compiled with help of CMake (in CLion):
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
typedef struct ball {
double x, y, z, r;
} ball;
bool intersect(ball *a, ball *b) {
double d = sqrt(pow(a->x - b->x, 2) + pow(a->y - b->y, 2) + pow(a->z - b->z, 2));
return d <= a->r + b->r;
}
I've also got my CMakeLists, which specify -lm
as compiler option. It is also the last option:
cmake_minimum_required(VERSION 3.20)
project(untitled C)
set(CMAKE_C_STANDARD 99)
add_compile_options(-Wall -Werror -Wformat-security -Wignored-qualifiers -Winit-self -Wswitch-default -Wfloat-equal -Wpointer-arith -Wtype-limits -Wempty-body -Wno-logical-op -Wstrict-prototypes -Wold-style-declaration -Wold-style-definition -Wmissing-parameter-type -Wmissing-field-initializers -Wnested-externs -Wno-pointer-sign -Wno-unused-result -std=gnu99 -lm)
add_executable(untitled main.c)
However, when compiling, I get a bunch of undefined references:
Scanning dependencies of target untitled
[ 50%] Building C object CMakeFiles/untitled.dir/main.c.o
[100%] Linking C executable untitled
/usr/bin/ld: CMakeFiles/untitled.dir/main.c.o: in function `intersect':
/home/keddad/CLionProjects/untitled/main.c:10: undefined reference to `pow'
/usr/bin/ld: /home/keddad/CLionProjects/untitled/main.c:10: undefined reference to `pow'
/usr/bin/ld: /home/keddad/CLionProjects/untitled/main.c:10: undefined reference to `pow'
/usr/bin/ld: /home/keddad/CLionProjects/untitled/main.c:10: undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/untitled.dir/build.make:93: untitled] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/untitled.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/untitled.dir/rule] Error 2
gmake: *** [Makefile:124: untitled] Error 2
What am I doing wrong? I've read another questions on the topic, and I thought that as long as I had -lm
, and it is specified as a last compiler argument, it should work fine. However, looks like it's not the case here.