简体   繁体   English

将静态函数分离到 C 中的另一个(头文件?)文件中

[英]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.

相关问题 从C中的另一个文件链接静态函数 - Linking static function from another file in c C / C ++:头文件中的静态函数是什么意思? - C/C++: Static function in header file, what does it mean? 如果在头文件中声明了函数,则C中的单独编译会产生错误 - separate compilation in C gives error if function is declared in header file c中的#ifdef表示文件名,用于保护头文件中的静态功能 - #ifdef in c for filename for guarding static function in header file header 文件中的 static 内联 function 和 misra Z4A8A08F09D33.8457933ZB 规则问题 - static inline function in header file and misra c rule 8.5 problem 编译 C 程序时出错,该程序调用在 header 文件中声明并在单独的 cpp 文件中定义的 C++ function - Error compiling a C program which calls a C++ function declared in header file and defined in a separate cpp file 如何从另一个C文件调用静态函数? - How to call the static function from another c file? 将函数分隔为独立的标头,该标头不依赖标头内部的另一个函数? - Separating functions to a separate header that rely on another function not inside the header? 从main()访问单独文件中的静态函数 - Accessing static function in separate file from main() 通过包含其头文件使用另一个c文件中的函数 - Using function from another c file by including its header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM