簡體   English   中英

構建內核模塊時如何鏈接靜態庫?

[英]How do I link a static library when building a kernel module?

我想從現有文件foo.c和靜態庫support.a構建 Linux 內核模塊foo.ko support.a是從 Rust 編譯的,所以沒有support.c

我使用了以下 Makefile

KERNEL_DIR := /lib/modules/$(shell uname -r)/build

obj-m += foo.o
foo-obs += support.a

all:
    $(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules V=1

似乎support.a沒有鏈接; 有警告說從foo.c調用的函數(並在support.a實現)是未定義的。

更新 0Makefile適用於 Ubuntu LTS(我已經在 18.04 和 14.04 上測試過),但不適用於 Fedora(29/30)。 Fedora 的輸出是:

...
make -C /lib/modules/5.1.11-200.fc29.x86_64/build SUBDIRS=/public/Github/rustyvisor modules
make[1] : on entre dans le répertoire « /usr/src/kernels/5.1.11-200.fc29.x86_64 »
Makefile:205: ================= WARNING ================
Makefile:206: 'SUBDIRS' will be removed after Linux 5.3
Makefile:207: Please use 'M=' or 'KBUILD_EXTMOD' instead
Makefile:208: ==========================================
  LD [M]  /public/Github/rustyvisor/rustyvisor.o
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: /public/Github/rustyvisor/rustyvisor.o(.init.text+0xbb): Section mismatch in reference from the function init_module() to the function .exit.text:rustyvisor_exit()
The function __init init_module() references
a function __exit rustyvisor_exit().
This is often seen when error handling in the init function
uses functionality in the exit path.
The fix is often to remove the __exit annotation of
rustyvisor_exit() so it may be used outside an exit section.

WARNING: "rustyvisor_core_unload" [/public/Github/rustyvisor/rustyvisor.ko] undefined!
WARNING: "rustyvisor_load" [/public/Github/rustyvisor/rustyvisor.ko] undefined!
WARNING: "rustyvisor_core_load" [/public/Github/rustyvisor/rustyvisor.ko] undefined!
WARNING: "rustyvisor_unload" [/public/Github/rustyvisor/rustyvisor.ko] undefined!
  LD [M]  /public/Github/rustyvisor/rustyvisor.ko
make[1] : on quitte le répertoire « /usr/src/kernels/5.1.11-200.fc29.x86_64 »

更新 1:有一個非常相似的問題,但這個問題是問為什么 Makefile 在 Fedora 上不起作用,但我發現它在 Ubuntu 上起作用。

最后,我找到了解決問題的方法(但我仍然不明白為什么)。 在 Fedora 上,庫名support.a應該更改為support.o ,然后鏈接器工作!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM