对 rand() 的未定义引用甚至认为它已被包含和定义

Undefined reference to rand() even thought it's included and defined

我正在为我们在 class 中使用的自定义 OS 开发 调度程序,但是当我尝试调用 rand()功能,它给了我这个错误:

In function `gen_rand_number':
sched.c:(.text+0x5e): undefined reference to `rand'

这是我的 sched.c 文件

#include <stdlib.h>
#include <signal.h>

PUBLIC int gen_rand_number(int min, int max) {
    int r;
    const int range = 1 + max - min;
    const int buckets = RAND_MAX / range;
    const int limit = buckets * range;  

    do {
        r = rand();
    } while (r >= limit);

    return min + (r / buckets);
}

所以经过大量谷歌搜索后,我意识到也许 stdlib 没有在这个 OS 上实现,但是我检查了一下

定义于:include/stdlib.h

并实现于:src/lib/libc/stdlib/rand.c 如下:

int rand(void)
{
    _next = (_next * 1103515245) + 12345;
    return ((_next >> 16) & 0x7fff);
}

我意识到这是一个非常具体的问题,但我希望有人能够帮助我。另外,如果需要更多信息,请告诉我。

If you want to look at the whole OS code

既然即将到来,我正在使用 makefile:

出口EDUCATIONAL_KERNEL=1

# Directories.
export BINDIR   = $(CURDIR)/bin
export SBINDIR  = $(BINDIR)/sbin
export UBINDIR  = $(BINDIR)/ubin
export DOCDIR   = $(CURDIR)/doc
export INCDIR   = $(CURDIR)/include
export LIBDIR   = $(CURDIR)/lib
export DOXYDIR  = $(CURDIR)/doxygen
export SRCDIR   = $(CURDIR)/src
export TOOLSDIR = $(CURDIR)/tools

# Toolchain
export CC = $(TARGET)-gcc
export LD = $(TARGET)-ld
export AR = $(TARGET)-ar

# Random number for chaos.
export KEY = 13

# Toolchain configuration.
export CFLAGS    = -I $(INCDIR)
export CFLAGS   += -DKERNEL_HASH=$(KEY) -DEDUCATIONAL_KERNEL=$(EDUCATIONAL_KERNEL)
export CFLAGS   += -std=c99 -pedantic-errors -fextended-identifiers
export CFLAGS   += -nostdlib -nostdinc -fno-builtin -fno-stack-protector
export CFLAGS   += -Wall -Wextra -Werror
export CFLAGS   += -Wstack-usage=3192 -Wlogical-op
export CFLAGS   += -Wredundant-decls -Wvla
export ASMFLAGS  = -Wa,--divide,--warn
export ARFLAGS   = -vq
export LDFLAGS   = -Wl,-T $(LIBDIR)/link.ld

# Resolves conflicts.
.PHONY: tools

# Builds everything.
all: nanvix documentation

# Builds Nanvix.
nanvix:
    mkdir -p $(BINDIR)
    mkdir -p $(SBINDIR)
    mkdir -p $(UBINDIR)
    cd $(SRCDIR) && $(MAKE) all

# Builds system's image.
image: $(BINDIR)/kernel tools
    mkdir -p $(BINDIR)
    bash $(TOOLSDIR)/build/build-img.sh $(EDUCATIONAL_KERNEL)

# Builds documentation.
documentation:
    doxygen $(DOXYDIR)/kernel.config

# Builds tools.
tools:
    mkdir -p $(BINDIR)
    cd $(TOOLSDIR) && $(MAKE) all

# Cleans compilation files.
clean:
    @rm -f *.img
    @rm -rf $(BINDIR)
    @rm -rf $(DOCDIR)/*-kernel
    cd $(SRCDIR) && $(MAKE) clean
    cd $(TOOLSDIR) && $(MAKE) clean

您需要 link 在您自己的 c 库实现中有点像这样:

$ gcc -LPATH -lc $object_files -o executable

其中 PATH 是你自己的 libc 的相对路径

或者...您可能最好听听 David Schwartz 的话,他已经指出,您必须自己实施 rand。这里的意思是:你需要在内核中实现它,因为我猜,sched.c是一个属于你内核的文件,所以stdclib函数对你不可用。

由于您构建的是独立 OS,而不是应用程序,因此您无权访问标准库。如果您查看 makefile,您会看到它将 -nostdlib -nostdinc 传递给编译器和链接器。如果您的代码库提供了 PRNG,您就可以使用它。否则,您需要添加一个实现。