如何防止“错误:此处未声明‘符号’"尽管在 Linux 内核模块中有 EXPORT_SYMBOL? [英] How to prevent "error: 'symbol' undeclared here" despite EXPORT_SYMBOL in a Linux kernel module?

查看:25
本文介绍了如何防止“错误:此处未声明‘符号’"尽管在 Linux 内核模块中有 EXPORT_SYMBOL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我收到此错误时,我正在将一些驱动程序嵌入到 Linux 内核中(我正在板文件中添加设备并注册它):

I'm embedding some driver into a Linux kernel when I get this error (I'm adding the device in the board file and registering it):

error: 'kxtf9_get_slave_descr' undeclared here (not in a function)

我在驱动文件中找到了上面的函数

I located the function above in a driver file

struct ext_slave_descr *kxtf9_get_slave_descr(void)
{
    return &kxtf9_descr;
}
EXPORT_SYMBOL(kxtf9_get_slave_descr);

不应该通过 EXPORT_SYMBOL 使其可见"吗?包含上面代码的C文件没有头文件(我没有写,我只是找到了这里和我执行他们说这是测试,因此我假设不需要标题?

Shouldn't it made "visible" by EXPORT_SYMBOL? The C file containing the code above has no header file (I didn't write it, I just found it here and I'm implementing. They say it's tested so I assume an header is not needed?

其余代码完美编译(因此它看到"文件夹中的代码),并且包含上述代码的文件也能编译!

The rest of the code compiles perfectly (so it "sees" the code in the folder), and the file containing the code above compiles as well!

推荐答案

EXPORT_SYMBOL 为动态链接导出符号.您所拥有的不是链接错误,而是由于缺少函数声明而导致的编译错误.您必须为 C 文件编写一个头文件并包含该头文件,或者您将函数声明为您正在编译的 C 文件.

EXPORT_SYMBOL exports the symbol for dynamic linking. What you have is not a linking error but a compilation error due to a missing function declaration. You have to either write a header file for the C file and include that header file, or you declare the function the C file you're compiling.

选项 1:

kxtf9.h:

#ifndef KXTF9_H
#define KXTF9_H

struct ext_slave_descr *kxtf9_get_slave_descr(void);

#endif

your_file.c:

your_file.c:

#include "kxtf9.h"
/* your code where you use the function ... */

选项 2:

your_file.c:

your_file.c:

struct ext_slave_descr *kxtf9_get_slave_descr(void);
/* your code where you use the function ... */

另请注意,文件 kxtf9.c 中的 EXPORT_SYMBOL 周围有 #ifdef __KERNEL__,因此您必须正确设置构建环境 (Makefile) -否则你会收到链接错误.

Also note that the EXPORT_SYMBOL in the file kxtf9.c has #ifdef __KERNEL__ around it, so you have to have set up your build environment (Makefile) correctly - otherwise you'll get a link error.

这篇关于如何防止“错误:此处未声明‘符号’"尽管在 Linux 内核模块中有 EXPORT_SYMBOL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆