# How to compile kernel module for Pynq v3

**URL:** <https://discuss.pynq.io/t/how-to-compile-kernel-module-for-pynq-v3/5870>\
**Category:** Support\
**Created:** [July 25, 2023, 8:17am UTC](https://discuss.pynq.io/t/how-to-compile-kernel-module-for-pynq-v3/5870 "2023-07-25T08:17:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KesM](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/kesm/32/4339_2.png) [@KesM](https://discuss.pynq.io/u/KesM)\
**Post date:** [July 25, 2023, 8:17am UTC](https://discuss.pynq.io/t/how-to-compile-kernel-module-for-pynq-v3/5870/1 "2023-07-25T08:17:34Z")

</div>

- PYNQ version: v3.0.1
- Board name: Pynq-Z2

Amazing work, thanks for your time on this project for all open source developers.  
I am trying to build in-tree kernel module ([SPI LCD](https://github.com/torvalds/linux/blob/master/drivers/gpu/drm/tiny/ili9341.c)). I see there are [Pynq bsp recipies](https://github.com/Xilinx/PYNQ/blob/master/boards/Pynq-Z2/petalinux_bsp/meta-user/recipes-bsp/u-boot/files/0004-add-pynqz2-support.patch), that contains patch example for kernel config file. I’ve spent many hours of trying to figure things out, maybe somebody could give some clues.

**Question:** What is recommended general way of building in-tree kernel module? Is it possible to build it without rebuilding whole system image?

---

<div class="post-metadata">

**Author:** ![RogerPease](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/rogerpease/32/6592_2.png) [@RogerPease](https://discuss.pynq.io/u/RogerPease)\
**Post date:** [January 15, 2025, 8:45pm UTC](https://discuss.pynq.io/t/how-to-compile-kernel-module-for-pynq-v3/5870/2 "2025-01-15T20:45:07Z")

</div>

Realize this question is 18 months old but I was having the same issue and didn’t see a reply. I tried to compile natively on the Xilinx board and got the asm/bitsperlong.h error. Also tried the sd card rebuild and chewed up my month’s ISP data allowance.

Xilinx publishes the linux distros at [GitHub - Xilinx/linux-xlnx: The official Linux kernel from Xilinx](https://github.com/Xilinx/linux-xlnx/) After a few educated guesses I was able to find the version that matched uname -r from my Pynq Z2 (v3.0.1 in my case).

Get the **linux-xlnx-xlnx\_rebase\_v5.15\_LTS\_2022.1\_update2.tar.gz** (or appropriate for your version) from the github repo and untar it into your desk/laptop. I am assuming you are cross-compiling.

cd into ./linux-xlnx-xlnx\_rebase\_v5.15\_LTS\_2022.1\_update2  
You can use **make kernelversion** to verify version base number.  
The -xilinx-v2022.1 is a .config variable CONFIG\_LOCAL\_VERSION  
Copy the .config from xilinx-board /lib/modules/`uname -r`/build to .config in this directory.

Then run:

**ARCH=arm CROSS\_COMPILE=arm-linux-gnueabi- make**  
**ARCH=arm CROSS\_COMPILE=arm-linux-gnueabi- make modules**

You may need to install the cross compiler and build essentials… If you hit an error from that Google is your friend.

Then to make your own module, update your module’s Makefile:

```auto
KERNEL_DIR=/path/to/linux-xlnx-xlnx_rebase_v5.15_LTS_2022.1_update2

all:
        ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- make -C $(KERNEL_DIR) M=$(shell pwd) modules

```

Hope this helps. I can’t promise you can rebuild the whole kernel this way because there may be patches for the modules.

---

<div class="post-metadata">

**Author:** ![pynqzen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/pynqzen/32/97_2.png) [@pynqzen](https://discuss.pynq.io/u/pynqzen)\
**Post date:** [January 16, 2025, 5:40am UTC](https://discuss.pynq.io/t/how-to-compile-kernel-module-for-pynq-v3/5870/3 "2025-01-16T05:40:41Z")

</div>

Hi, sorry for missing this question for such a long time. You have to make sure when you build any kernel module that you are using the same headers and #defines that the pynq build process sets up.

This is an example that uses an out of petalinux tree module but I believe in-tree would be similar. Hope this helps, see here: [Ultra96-PYNQ/Ultra96/packages/wilc3000/pre.sh at master · Avnet/Ultra96-PYNQ · GitHub](https://github.com/Avnet/Ultra96-PYNQ/blob/master/Ultra96/packages/wilc3000/pre.sh)

Kind regards
