How to fix error: implicit declaration of function ‘setup_timer’

How to fix error: implicit declaration of function ‘setup_timer’

正在尝试为 usb-audio 卡 Line6 UX2 编译 linux 核心模块。 从回购中获取代码并尝试制作。回购没有 ./configure

生成错误:

line6linux-code-r1108/driver/trunk/driver.c:169:2: error: implicit declaration of function ‘setup_timer’; did you mean ‘del_timer’? [-Werror=implicit-function-declaration]
  setup_timer(timer, function, data);
  ^~~~~~~~~~~
  del_timer

C 函数 setup_timer() 在 linux linux-headers 包中声明。目前是 linux-headers-4.18.0-16 当然默认安装在/usr/src/

产生错误的代码 (driver.c):

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/timer.h>
#include <linux/export.h>
#include <linux/slab.h>
#include <linux/usb.h>

#include <sound/core.h>
#include <sound/initval.h>

#include "capture.h"
#include "driver.h"
#include "midi.h"
#include "playback.h"

... some code here

/*
    Setup and start timer.
*/
void line6_start_timer(struct timer_list *timer, unsigned long msecs,
               void (*function)(unsigned long), unsigned long data)
{
    setup_timer(timer, function, data); // <-- string 169
    mod_timer(timer, jiffies + msecs_to_jiffies(msecs));
}
EXPORT_SYMBOL_GPL(line6_start_timer);

显然编译器找不到 linux headers。我该如何解决?

同样根据安装文件,driver 是为内核版本 linux-headers-2.6.x 编写的,现在是 4.18.0-16。

更多来自 INSTALL:对于其他发行版,请查阅文档以了解您必须安装哪个包。

一切都丢失了?

OS Ubuntu 18.10 Linux 4.18.0-16-generic #17-Ubuntu SMP Fri Feb 8 00:06:57 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

在较新版本的 Linux 内核中,setup_timer 函数称为 timer_setup

请注意,回调函数的类型在最新版本中已更改。现在回调接受指向 timer_list 结构本身的指针。

在这篇 LWN 文章中查看更多信息:https://lwn.net/Articles/735887/