---
# System prepended metadata

title: Compile Linux Kernel
tags: [Linux Kernel, env-setup, linux, kernel]

---

# Compile Linux Kernel
###### tags: `linux` `kernel` `env-setup` `Linux Kernel`
Host: Linux Ubuntu-20.04
Target Platform: vexpress (arm cortex-a9)
Linux Kernel Version running on target platform: 5.10.1

## Prev Step

[Busybox: build minimal file system](https://hackmd.io/wFUATcWyT-epSdbwgYUeAw)

## Setting kernel config before build

在 linux-5.10.1 路徑下:

#### Modify vexpress_defconfig

用編輯器打開 ./arch/arm/config/vexpress_defconfig
將 CONFIG_CMDLINE 的內容清空
然後新增:
CONFIG_INITRAMFS_SOURCE="_install"
CONFIG_VMSPLIT_3G=y
CONFIG_HIGHMEM=y

像是這樣:

	CONFIG_CMDLINE=""
	CONFIG_INITRAMFS_SOURCE="_install"
	CONFIG_VMSPLIT_3G=y
	CONFIG_HIGHMEM=y


#### Build Linux Kernel to specific dir
使用 export KBUILD_OUTPUT=./build 指定build的位置, 如 ./build下: 


把 linux-5.10.1 下的 _install 資料夾 copy 到 build 下:

	sudo cp -r ./_install ./build

/\*Set Config\*/

	export KBUILD_OUTPUT=./build; \
	export ARCH=arm; \
	export CROSS_COMPILE=arm-linux-gnueabi-; \
	make vexpress_defconfig


如果有 modify vexpress_defconfig, 進入 menuconfig 後不做任何動作直接exit就好

	make menuconfig



## Build Linux Kernel

/\*Build\*/

	export KBUILD_OUTPUT=./build; \
	make bzImage -j4 ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-; \
	
```
# If bear has been insalled:
sudo apt install bear

# build
bear make bzImage -j8 KCFLAGS=-DDEBUG ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
```

/\*Build dts\*/

	export KBUILD_OUTPUT=./build; \
	make dtbs -j4 ARCH=arm


要開啟 debug 可以加上:

	KCFLAGS=-DDEBUG


## Clean Build

	export KBUILD_OUTPUT=./build; \
	make clean ARCH=arm; \
	make mrproper ARCH=arm
	
或直接把 linux-5.10.1 下的 build 資料夾刪掉

	sudo rm -r ./build/*

重新 build 要把 _install copy 回 build 下:

	sudo cp -r ./_install/ ./build/


## Trouble shooting

1./bin/sh: 1: flex: not found
    
    sudo apt install flex
    
2./bin/sh: 1: bison: not found

    sudo apt install bison
	
3.fatal error: openssl/bio.h: No such file or directory

    sudo apt install libssl-dev


## Ｎext Step

[Run Linux Kernel on QEMU emulator](https://hackmd.io/ikVQ_oeqSoaWsaz8Q-E6qQ)