Skip to main content

用户态程序编写

这一步就很简单了, 只需要再补充一些交叉编译的知识

#include<stdio.h>

int main() {
printf("Hello, world!\n");
return 0;
}

用已经生成好的工具链编译并把结果丢到build/ramdisk文件夹里面

./build/chcore-libc/bin/musl-gcc ./user/apps/my-apps/hello.c -o ./build/ramdisk/my-hello.bin

然后 ./chbuild rambuild 重新生成内核镜像

./build/simulate.sh 重新进入chcore

就能看到我们的my-hello啦

hello-world 你甚至可以编译一个最小的busybox进去LOL

mini-busybox

具体参见https://www.busybox.net/FAQ.html

下载(由于container 里面没有git, 需要在外部下好之后rebuild或者chown给container)

git clone --depth 1 https://git.busybox.net/busybox
cd busybox

然后

make allnoconfig 

禁用busybox的所有模块(因为强绑定linux)

然后

make menuconfig

选择和linux不强相关的模块(没有对fs等的假设,例如chcore默认不带/proc),例如cat, echo等,然后编译

 # 在chcore目录下
export PATH=$PATH:$(pwd)/build/chcore-libc/bin
# 在busybox目录下
LDFLAGS="--static" make -j 4 CROSS_COMPILE="musl-" # 使用chcore预构建的musl交叉静态编译

然后将目录copy到ramdisk下, ./chbuild rambuild 重新生成内核镜像

./build/simulate.sh 重新进入chcore, 就可以如上图使用busybox啦