gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:34:50 25 4
gpt4 key购买 nike

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

Settings -> Build, Execution and Deployment -> Toolchains CLion 说 CMake 版本是 3.15.3 GDB版本是8.3没关系。我已经厌倦了重新安装 GCC、CMake 和 CLion,但它不起作用。我也厌倦了在 StackOverflow 上搜索一些信息,但仍然没有任何效果。

主.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

我需要编译我的项目。

最佳答案

数学库通常作为您明确需要链接的单独库(方便地命名为 m)进行链接。

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

target_link_libraries(untitled2 m)

关于c - Linux 上 CLion 中的数学库链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58818529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com