我正在为我们在类中使用的自定义操作系统处理调度程序,但是当我尝试调用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不是在这个操作系统上实现的,但是我检查了
定义于:include/stdlib.h
并在:src/lib/lib c/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:
导出教育内核=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
最佳答案
因为您构建的是独立的操作系统,而不是应用程序,所以您无法访问标准库。如果您查看makefile,就会看到它将-nostdlib -nostdinc
传递给编译器和链接器。如果你的代码库提供了一个PRNG,你可以使用它。否则,需要添加一个实现。
关于c - 对rand()的 undefined reference 甚至认为已包含并定义了它,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40353201/