关于Ubuntu Linux系统下使用C的有关问题
关于Ubuntu Linux系统下使用C的<math.h>问题
向大家请教一个问题:
第一个程序可以编译通过:
第二个程序不能编译通过:
为什么在sqrt( )函数中,参数必须要写具体的实数,不能使用变量名呢?
如果不能用变量名,怎么实现变量值可以代入实参位置实现计算?
在线等待回复,最迟3小时内结贴。
------解决方案--------------------
直接gcc test04-03.c -lm
执行那个a.exe看看
------解决方案--------------------
·-l参数和-L参数
-l参数就是用来指定程序要链接的库,-l参数紧接着就是库名,那么库名跟真正的库文件名有什么关系呢?就拿数学库来说,他的库名是m,他的库文件名是libm.so,很容易看出,把库文件名的头lib和尾.so去掉就是库名了
好了现在我们知道怎么得到库名,当我们自已要用到一个第三方提供的库名字libtest.so,那么我们只要把libtest.so拷贝到/usr/lib里,编译时加上-ltest参数,我们就能用上libtest.so库了(当然要用libtest.so库里的函数,我们还需要与libtest.so配套的头文件)
向大家请教一个问题:
第一个程序可以编译通过:
- C/C++ code
1 // P95: 2.编程题 (6)调用库函数编程求表达式cos(39.5度)+(e的2.916次幂)+(3.16的算数方根) 2 3 // The begining of C program: test03-06.c. 4 5 // Header files included. 6 #include <stdio.h> 7 #include <math.h> 8 9 // Macro definition. 10 #define PI 3.1415926 11 12 // Main function's declaration. 13 int main ( void ) 14 { 15 long int temp=0; 16 float result=0.0; 17 result = cos(39.5*PI/180) + exp(2.916) + sqrt(3.16); 18 temp = (long int) (result * 100); 19 temp = (long int) ((result*100 - temp)*2 + temp); 20 result = temp / 100.0; 21 printf ( "Result: cos(39.5*PI/180) + exp(2.916) + sqrt(3.16) = %.2lf\n", 22 result ); 23 24 return (0); 25 } 26 27 // The end of C program: test03-06.c. 28
第二个程序不能编译通过:
- C/C++ code
1 // P120: 3.编程题 (3) 2 3 // The begining of C program: test04-03.c. 4 5 // Header files included. 6 #include <stdio.h> 7 #include <math.h> 8 9 // Main function's declaration. 10 int main ( void ) 11 { 12 long int i_tag=0; 13 float f_num1, f_num2, f_num3; 14 double area=0.0, temp=0.0; 15 printf ( "Input the three side's length of triangle: " ); 16 scanf ( "%f %f %f", &f_num1, &f_num2, &f_num3 ); 17 if ( (f_num1<=0) || (f_num2<=0) || (f_num3<=0) ) 18 { 19 printf ( "Input error, data is illegal!\n" ); 20 return (1); 21 } 22 if ( ((fabs(f_num2-f_num3) < f_num1) && (f_num1 < (f_num2+f_num3))) || 23 ((fabs(f_num1-f_num3) < f_num2) && (f_num2 < (f_num1+f_num3))) || 24 ((fabs(f_num1-f_num2) < f_num3) && (f_num3 < (f_num1+f_num2))) ) 25 { 26 printf ( "The three sides can form a triangle.\n" ); 27 temp = ( f_num1 + f_num2 + f_num3 ) / 2; 28 area = temp*fabs(f_num1-temp)*fabs(f_num2-temp)*fabs(f_num3-temp); 29 area = sqrt(area); 30 31 // 对所求结果四舍五入并保留到小数点后两位之后输出 32 i_tag = (long int) (area * 100); 33 i_tag = (long int) ((area*100 - i_tag)*2 + i_tag); 34 area = i_tag / 100.0; 35 printf ( "The triangle's area is: %-.2lf\n", area ); 36 37 return (0); 38 } 39 else 40 printf ( "The three sides can't form a triangle.\n" ); 41 42 return (0); 43 } 44 45 // The end of C program: eg04-03.c. 46
为什么在sqrt( )函数中,参数必须要写具体的实数,不能使用变量名呢?
如果不能用变量名,怎么实现变量值可以代入实参位置实现计算?
在线等待回复,最迟3小时内结贴。
------解决方案--------------------
直接gcc test04-03.c -lm
执行那个a.exe看看
------解决方案--------------------
·-l参数和-L参数
-l参数就是用来指定程序要链接的库,-l参数紧接着就是库名,那么库名跟真正的库文件名有什么关系呢?就拿数学库来说,他的库名是m,他的库文件名是libm.so,很容易看出,把库文件名的头lib和尾.so去掉就是库名了
好了现在我们知道怎么得到库名,当我们自已要用到一个第三方提供的库名字libtest.so,那么我们只要把libtest.so拷贝到/usr/lib里,编译时加上-ltest参数,我们就能用上libtest.so库了(当然要用libtest.so库里的函数,我们还需要与libtest.so配套的头文件)