Linux 上 CLion 中的数学库链接错误
Math library linking error in CLion on Linux
为了完成我的作业,我需要 #include "math.h"
,但是在更新 GCC 和 CMake 之后,CLion 无法 link 我的项目文件。我应该怎么做才能解决这个问题?
在Settings -> Build, Execution and Deployment -> Toolchains
中,CLion说CMake版本是3.15.3
,GDB版本是8.3
,没问题。
我已经厌倦了重新安装 GCC、CMake 和 CLion,但它没有用。我也厌倦了在 Whosebug 上搜索一些信息,但仍然没有任何效果。
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
我需要编译我的项目。
数学库通常 link 作为您明确需要 link 使用的单独库(方便命名 m
)编辑。
您使用 target_link_libraries
命令告诉 CLion(通过其 CMakeLists.txt
文件)link 与库:
target_link_libraries(untitled2 m)
为了完成我的作业,我需要 #include "math.h"
,但是在更新 GCC 和 CMake 之后,CLion 无法 link 我的项目文件。我应该怎么做才能解决这个问题?
在Settings -> Build, Execution and Deployment -> Toolchains
中,CLion说CMake版本是3.15.3
,GDB版本是8.3
,没问题。
我已经厌倦了重新安装 GCC、CMake 和 CLion,但它没有用。我也厌倦了在 Whosebug 上搜索一些信息,但仍然没有任何效果。
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
我需要编译我的项目。
数学库通常 link 作为您明确需要 link 使用的单独库(方便命名 m
)编辑。
您使用 target_link_libraries
命令告诉 CLion(通过其 CMakeLists.txt
文件)link 与库:
target_link_libraries(untitled2 m)