【发布时间】:2016-11-01 02:09:29
【问题描述】:
我正在为我们在课堂上使用的自定义操作系统调度程序工作,但是当我尝试调用 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/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
【问题讨论】:
-
收到该错误消息时使用了什么命令?
-
发布了 makefile @DavidSchwartz
-
您应该在帖子顶部添加一个大红色闪烁警告灯,表明您不是在普通 C 环境中工作,而是在嵌入式环境中工作。
-
对不起@BodoThiesen,这是帖子的第一句话,但我会把它变成红色的
-
没有问题,我只是没有意识到。
标签: c header-files scheduler