当我尝试编译它时,它显示“testrand.c:(.text+0x11): undefined reference to `rand_number'”
1 #include <stdio.h>
2 #include <time.h>
3
4 int rand_number(int param);
5
6 main()
7 {
8 while(5)
9 {
10 printf("%d", rand_number(15));
11 sleep(1);
12 }
13
14
15 int rand_number(int param)
16 {
17 srand((unsigned int)time(NULL));
18 int x = param;
19 int rn = rand() % x;
20 return rn;
21 }
22 }
但是,我已经在上面清楚地定义了它......
我试过将 time.h 包括在引号中,包括 stdlib.h 等...但仍然不知道发生了什么。有人知道发生了什么吗?
最佳答案
发生这种情况是因为您的 rand_number
函数是在其他函数 main
中定义的。
这应该可以解决您的问题:
#include <stdio.h>
#include <time.h>
int rand_number(int param);
main()
{
while(5)
{
printf("%d", rand_number(15));
sleep(1);
}
}
int rand_number(int param)
{
srand((unsigned int)time(NULL));
int x = param;
int rn = rand() % x;
return rn;
}
关于c - 我 undefined reference 已定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10447078/