Linux上CLion中的数学库链接错误

要做作业,我需要#include "math.h",但是在更新GCC和CMake之后,CLion无法链接我的项目文件。我该怎么做才能解决此问题?

Settings -> Build,Execution and Deployment -> Toolchains中,CLion表示CMake版本为3.15.3,GDB版本为8.3,可以。 我已经厌倦了重新安装GCC,CMake和CLion,但是没有用。我也很累,无法在StackOverflow上搜索一些信息,但是仍然没有任何效果。

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)

编译器输出:

====================[ 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

我需要编译我的项目。

chen20060450 回答:Linux上CLion中的数学库链接错误

数学库通常作为一个单独的库(通常名为m)链接,您明确需要与其链接。

您通过target_link_libraries命令告诉CLion(通过其CMakeLists.txt文件)与库链接:

target_link_libraries(untitled2 m)
本文链接:https://www.f2er.com/3117534.html

大家都在问