C-Prog Code::Blocks -- math.h 库未在 IDE 中链接...在命令行中工作正常要在 IDE 中链接的文件/文件夹在哪里?

问题描述

NEWB here:使用 Linux Mint 20.x - Code::Blocks - GCC --- 我让这段代码在命令行上工作。直到我使用 链接数学库后,它才起作用。我试图在 Code::Blocks 中链接数学库以在 IDE 中运行代码。我在 Code::Blocks 中找不到要链接的库文件夹/文件

旁注:我添加 sqrt 只是为了测试数学。 sqrt 可以在 IDE 中使用,但 floor 和 ceil 不行。我觉得有些奇怪,有些 math.h 函数可以工作,而有些则不行。

gcc tut36.c -o tut36 -lm 
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main(){

double bacon1 = 9.65345;
double bacon2 = 3.3;


printf("bacon1 is %.2lf\n",floor(bacon1));
printf("bacon2 is %.2lf\n",floor(bacon2));

printf("bacon1 is %.2lf\n",ceil(bacon1));
printf("bacon2 is %.2lf\n",ceil(bacon2));


int year1;
int year2;
int age;

printf("Enter a year\n");
scanf(" %d",&year1);

printf("Enter another year\n");
scanf(" %d",&year2);

age = year1 - year2;
age = abs(age);

printf("Your age is: %d %.0lf\n",age,sqrt(16));




return 0;
}

解决方法

找到解决方案:在 CODE::BLOCKS .... -lm 必须插入到编译器设置中。设置 -> 编译器 -> 链接器设置(选项卡) -> 其他链接器选项 -> -lm

-lm 显然将数学库链接到项目。如果数学库是标准库的一部分,不知道为什么这是必要的,但是你去了。