我想为系统调用处理程序创建一个接口(interface),但我不想在头文件中声明实现它的函数。
我希望它们是静态
,在syscall.c
(源文件)中,而不是在syscall..h
中声明(头文件)。
有什么办法可以实现这一点吗? 另外,您知道有哪些涉及此主题的 C 书籍吗?
//container of pointers to the functions executing the system call
typedef struct
{
void (*halt) (void);
void (*exit) (int status);
pid_t (*exec) (const char *file);
int (*wait) (pid_t pid);
bool (*create) (const char *file, int initial_size);
bool (*remove) (const char *file);
int (*open) (const char *file);
int (*filesize) (int fd);
int (*read) (int fd, void *buffer, int length);
int (*write) (int fd, const void *buffer, int length);
void (*seek) (int fd, int position);
int (*tell) (int fd);
void (*close) (int fd);
}td_sys_call_handler;
//points to system call handling functions
static td_sys_call_handler syscall_redirect =
{
.halt = fu_halt,
.exit = fu_exit,
.exec = fu_exec,
.wait = process_wait,
.create = fu_create,
.remove = fu_remove,
.open = fu_open,
.filesize = fu_file_filesize,
.read = fu_read,
.write = fu_write,
.seek = fu_file_seek,
.tell = fu_file_tell,
.close = fu_file_close,
};
最佳答案
在标题中声明:
extern td_sys_call_handler syscall_redirect;
在实现 (syscall.c
) 中,只需将结构定义为普通全局变量 - 但不要将其设置为静态
。 (static
变量具有内部链接,因此它不能被其他模块引用,但此结构的全部目的是供其他模块使用它。)
关于C 将接口(interface)放在头文件中,同时隐藏实现它的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22423811/