c - 我 undefined reference 已定义

标签 c undefined-reference

当我尝试编译它时,它显示“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/

相关文章:

c - 嵌入式系统中的 malloc

编译器找不到所需的 SSL 库 - undefined reference

c++ - 在 Windows 64 中编译 QT - undefined reference

C 编程 - "Undefined symbol referenced in file"

c - 在c中使用删除函数

c - 在 C 中,如何写入特定的内存位置,例如视频内存 b800,在 DOS 中(真正的 DOS,MS DOS 6.22)

c - Vigenere CS50 - 需要帮助循环浏览字母

c - 将 double 转换为 int 的安全方法是什么?

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c++ - linux 链接到 .so ,但仍然得到 undefined reference