C 将接口(interface)放在头文件中,同时隐藏实现它的函数

标签 c

我想为系统调用处理程序创建一个接口(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/

相关文章:

java - 关于创建非标准大小的字节组的非常简单的问题

c - 限制 C 中的库链接

读取单独的字符串时出现 C 段错误

c - if 条件中的 printf 语句

c - 从 c 中的链表中删除重复项?

c - 是否保证相同类型的值之间没有填充?

c - 为什么负长值大于正无符号长值?

c - 为什么第二个 malloc 在这种情况下会失败?

正在运行的 C 程序可以访问它自己的符号表吗?

c - EOF 上的意外 fgets 行为