RunC和math.h的问题
可能重复:RunC和math.h的问题,c,runc,C,Runc,可能重复: 我正在使用RunC编写一个简单的函数,它需要pow和floor/truncate。我包括数学。当我使用主要功能时,没有问题。然而,一旦我尝试创建一个单独的int函数,RunC突然没有pow和floor函数,并给了我一个错误。有什么帮助吗 代码如下:main()可以工作,但如果我将其切换为使用上面的函数执行完全相同的操作,它将无法工作 #include <stdio.h> #include <math.h> int sumofsquares(int x){
我正在使用RunC编写一个简单的函数,它需要pow和floor/truncate。我包括数学。当我使用主要功能时,没有问题。然而,一旦我尝试创建一个单独的int函数,RunC突然没有pow和floor函数,并给了我一个错误。有什么帮助吗 代码如下:main()可以工作,但如果我将其切换为使用上面的函数执行完全相同的操作,它将无法工作
#include <stdio.h>
#include <math.h>
int sumofsquares(int x){
int counter = 0;
int temp = x;
while (temp != 0 || counter == 100){
//temp = temp - (int)pow(floor(sqrt(temp)), 2);
//temp = temp - pow(temp, 0.5);
printf("%d\n", temp);
counter = counter + 1;
}
/*while(temp != 0){
temp = temp - (int)pow(floor(sqrt(temp)), 2);
counter ++;
}*/
return counter;
}
int main(void){
printf("%d", (int)pow(floor(sqrt(3)), 2));
}
使用gcc编译程序:
gcc -lm -o foo foo.c
在工作
main
功能中,您有
printf("%d", (int)pow(floor(sqrt(3)), 2));
请注意,这里的参数是常量。优化编译器通常会在编译时对表达式求值,从而消除对math.h
函数的调用,因此即使不链接数学库,它也能工作。但是,如果计算涉及变量,通常无法在编译时对其进行计算,因此对math.h
函数的调用将保持不变,如果没有在数学库中进行链接,链接将失败。试一试
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
// don't really use atoi, it's here just for shortness
int num = argc > 1 ? atoi(argv[1]) : 3;
printf("%d\n", (int)pow(floor(sqrt(num)),2));
return EXIT_SUCCESS;
}
要链接的库应该放在命令行的最后,因为对于许多版本来说,如果在知道需要哪些符号之前就指定了它们,那么这些库将不起作用
不幸的是,我一点都不懂RunC,所以我还不能告诉你如何在数学库中链接它,我正在尝试找出答案
我的google fu太弱了。我还没有找到任何关于RunC的有用文档,我也不打算安装Ubuntu来检查工具本身。你能补充一下你到目前为止所做的工作吗?你在这里问的问题与你的问题基本相同吗?你的RunC环境似乎已经被水淹没了。我想问一下,为什么你使用RunC而不是已经存在于你的ubuntuvm上的gcc?这是我大学课程的一部分。我们在RunC环境中学习C语言,并希望使用它。目前,我必须为Ubuntu使用VM,这样我才能在我的Windows笔记本电脑上编写代码。Ubuntu上的RunC可能有问题吗?我不知道如何使用gcc。我必须使用RunC,因为提交服务器希望我使用RunC。
printf("%d", (int)pow(floor(sqrt(3)), 2));
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
// don't really use atoi, it's here just for shortness
int num = argc > 1 ? atoi(argv[1]) : 3;
printf("%d\n", (int)pow(floor(sqrt(num)),2));
return EXIT_SUCCESS;
}
gcc -O3 -Wall -Wextra -o foo foo.c -lm