1

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.

1 Answers1

3

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)
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    @ВадимСамохвалов You create the target `untitled2` with the `add_executable` command. You can put it anywhere below the target creation. – Some programmer dude Nov 12 '19 at 12:22