首页
/
每日頭條
/
科技
/
linux執行makefile的命令
linux執行makefile的命令
更新时间:2024-09-28 19:21:14

Linux内核源碼文件繁多,搞不清Makefile、Kconfig、.config間的關系,不了解内核編譯體系,編譯修改内核有問題無從下手,自己寫的驅動不知道怎麼編進内核,不知道怎麼配置内核,這些問題都和Makefile、Kconfig、.config有關,下面簡單談談Makefile、Kconfig和.config。希望對你有啟發。

嵌入式進階教程分門别類整理好了,看的時候十分方便,由于内容較多,這裡就截取一部分圖吧。

linux執行makefile的命令(技術幹貨淺談内核的Makefile)1

需要的朋友私信【内核】即可領取。

三者的作用

簡單來說就是去飯店點菜:Kconfig是菜單,Makefile是做法,.config就是你點的菜。

  • Makefile:一個文本形式的文件,編譯源文件的方法。
  • Kconfig:一個文本形式的文件,内核的配置菜單。
  • .config:編譯内核所依據的配置。
三者的語法

1,Makefile

參考:linux-3.4.2/drivers/Makefile

作用:用來定義哪些内容作為模塊編譯,哪些條件編譯等。子目錄Makefile被頂層Makefile包含。

(1)直接編譯obj-y = xxx.o

表示由xxx.c或xxx.s編譯得到xxx.o并直接編進内核。

(2)條件編譯obj -$(CONFIG_HELLO) = xxx.o

根據.config文件的CONFIG_XXX來決定文件是否編進内核。

(3)模塊編譯obj-m =xxx.o

表示xxx作為模塊編譯,即執行make modules時才會被編譯。

2、Kconfig

每個config菜單項都有類型定義: bool布爾類型、 tristate三态(内建、模塊、移除)、string字符串、 hex十六進制、integer整型。

作用:決定make menuconfig時展示的菜單項,

參考:linux-3.4.2/drivers/leds/ kconfig:

config LEDS_S3C24XX

tristate "LED Support for Samsung S3C24XX GPIO LEDs"

depends on LEDS_CLASS

depends on ARCH_S3C24XX

help

This option enables support for LEDs connected to GPIO lines

on Samsung S3C24XX series CPUs, such as the S3C2410 and S3C2440.

LEDS_S3C24XX:配置選項的名稱,省略了前綴"CONFIG_"

Tristate:表示該項是否編進内核、編成模塊。顯示為< > , 假如選擇編譯成内核模塊,則會在.config中生成一個 CONFIG_HELLO_MODULE=m的配置,選擇Y就是直接編進内核,會在.config中生成一個 CONFIG_HELLO_MODULE=y的配置項。Tristate後的字符串是make menuconfig時顯示的配置項名稱。

bool:此類型隻能選中或不選中,make menuconfig時顯示為[ ],即無法配置成模塊。

dependon:該選項依賴于另一個選項,隻有當選項被選中時,當前配置項的提示信息才會出現,才能設置當前配置項。

select:反向依賴關系,該選項選中時,同時選中select後面定義的那一項。

help:幫助信息。

目錄層次疊代:Kconfig中有類似語句:source "drivers/usb/Kconfig",用來包含(或嵌套)新的Kconfig文件,使得各個目錄管理各自的配置内容,不必把那些配置都寫在同一個文件裡,方便修改和管理。

3、.config

參考:linux-3.4.2/.config

通過前兩個文件的分析,.config的含義已經很清晰:内核編譯參考文件,查看裡面内容可以知道哪些驅動被編譯進内核。

配置内核的方式有3種(任選其一):

(1)make menuconfig

(2)make xxx_defconfig

(3)直接修改.config

注意: 如果直接修改.config,不一定會生效,因為有些配置可能存在依賴關系,make時會根據依賴關系,進行規則的檢查,直接修改.config有時無效,所以不推薦直接修改。

以上可能有點抽象,下面舉例說明:

寫一個簡單的入口函數輸出hello world的驅動并編譯進内核。

步驟:

(1)在drivers目錄下新建hello文件夾,裡面實現hello.c、Makefile、Kconfig。

hello.c:

#include #include #include static int first_drv_init(void ) { printk("------------------hello world !--------------------"); return 0; } static void first_drv_exit(void) { printk("------------------exit hello world !--------------------"); } module_init(first_drv_init); module_exit(first_drv_exit); MODULE_LICENSE("GPL");

Makefile:

obj-$(CONFIG_HELLO) = hello.o

Kconfig:

config HELLO

tristate "Hello World for fengyuwuzu"

help

Hello for fengyuwuzu

config HELLO決定名字:CONFIG_HELLO。

Hello World for fengyuwuzu:決定了在make menuconfig時顯示的名字

(2)修改上一級(Linux-3.4.2/drivers下)的Makefile、Kconfig。

Makefile:

obj-y = hello/

Kconfig:

source "drivers/hello/Kconfig"

(3)make menuconfig

(4)make uImage再燒寫到開發闆。

查看内核啟動Log,偉大的helloworld 出來了!說明hello.c成功編進内核

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
Copyright 2023-2024 - www.tftnews.com All Rights Reserved