[英]Separate static function into another (header?) file in C
I'm learning how to program in C and came across the following issue.我正在学习如何用 C 编程并遇到以下问题。 I have some private helper functions that I would not like to expose anywhere.
我有一些我不想在任何地方公开的私有帮助函数。 Here is how source files look like:
下面是源文件的样子:
logic.h
: logic.h
:
#ifndef LOGIC_H
#define LOGIC_H
enum logic_type{
customer_logic = 1,
requestor_logic,
company_logic
}
void execute_logic(enum logic_type logic_type);
#endif //LOGIC_H
logic.c
: logic.c
:
#include "logic.h"
static void _execute_customer_logic();
static void _execute_requestor_logic();
static void _execute_company_logic();
void execute_logic(enum logic_type logic_type){
switch(logic_type){
case customer_logic:
_execute_customer_logic();
break;
case requestor_logic:
_execute_requestor_logic();
break;
case company_logic:
_execute_company_logic();
break;
}
}
void _execute_customer_logic(){
//very long and complicated function
}
void _execute_requestor_logic(){
//very long and complicated function
}
void _execute_company_logic(){
//very long and complicated function
}
With the current design it looks really messy.以目前的设计,它看起来非常凌乱。 So I was thinking about putting each logic in a separate private header file and then simply include it.
所以我想把每个逻辑放在一个单独的私有头文件中,然后简单地包含它。 Like
喜欢
company_logic_internal.h
: company_logic_internal.h
:
#ifndef COMPANY_LOGIC_INTERNAL_H
#define COMPANY_LOGIC_INTERNAL_H
void _execute_company_logic(){
//very long and complicated function
}
#endif //COMPANY_LOGIC_INTERNAL_H
The problem is that I put an implementation into the header file which looks contradictory to the definition of header files which contains function prototypes, but not the actual definition.问题是我将一个实现放入头文件中,该实现看起来与包含函数原型的头文件的定义相矛盾,但与实际定义并不矛盾。 Making this
company_logic.h
to be a company_logic.c
also looks wierd since it means that .c
file will be included.使这个
company_logic.h
成为company_logic.c
看起来也很奇怪,因为这意味着.c
文件将被包含在内。
So I'm kind of confused of how to implement such a thing better.所以我对如何更好地实现这样的事情感到困惑。
This not exactly direct answer to the question, but you can try this to avoid unnecessary expose of your code and avoid recompile.这不是问题的直接答案,但您可以尝试这样做以避免不必要的代码暴露并避免重新编译。
so instead of going for number of header files(.h files), go for .c files, ie, each function can be defined in separate .c files and create static library
(ex: company_private.a), you haven't mentioned what compiler you use but with gcc
you can control access to the library with visibility
option or --exclude-libs
(though not sure it is supported with all tool chain) while compilation and link it to the program where you want use those API's.因此,不要选择头文件(.h 文件)的数量,而是选择 .c 文件,即每个函数都可以在单独的 .c 文件中定义并创建
static library
(例如:company_private.a),您没有提到您使用什么编译器,但使用gcc
您可以在编译时使用visibility
选项或--exclude-libs
(虽然不确定所有工具链都支持它)来控制对库的访问,并将其链接到您想要使用这些 API 的程序。
for simple toy example of creating static library check following link http://www.codingfleets.com/2018/12/how-to-create-static-library-in-c.html
对于创建静态库的简单玩具示例,请检查以下链接
http://www.codingfleets.com/2018/12/how-to-create-static-library-in-c.html
if you are using gcc tool chain, you can find lot of documentation about limiting visibility of symbols online.如果您使用 gcc 工具链,您可以在网上找到很多关于限制符号可见性的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.