使函数仅在库中可见,而不是在 API 中
Make function visible only within a library, not in API
我正在编写一个分布在多个文件中的 C99 库,例如
// core.h
void my_private_fn();
void API_my_public_fn();
// core.c
#include "core.h"
void my_private_fn() {
do_something();
}
void API_my_public_fn() {
do_something_else();
}
// module_a.h
#include "core.h"
void API_useful_thing();
// module_a.c
#include "module_a.h"
void API_useful_thing() {
my_private_fn();
}
我只希望 API_
前缀函数对使用该库的程序可见,但我还需要在 core.h
中公开 my_private_fn
以便 module_a.c
。在 C 中有没有办法让 my_private_fn
只在库中可见?
将它们放在仅在库内部使用而不分发给最终用户的内部头文件中——比如说,core_internal.h
。
如果函数必须只在定义它的编译单元中可见,那么您可以声明它static
。因为 C 语言提供的范围很少:一个符号只能有 3 个范围:
- 局部于块(块可以是函数或函数内的块)
- 静态作用域(函数外的静态声明):符号只在声明它的编译单元可见
- 全局范围(函数外的非静态声明):符号在整个程序中可见。
您最多可以隐藏 私有包含文件中您未在官方文档中声明的声明API。那样听话的用户不应该使用它。但是你不能阻止用户将声明放在自己的代码中并使用该函数。
我找到了一种更简洁的方法来根据我选择的 Serge 的答案来布置我的代码,它的最大优点是。
关键是将“私有”函数放在只包含在 C 文件中的头文件中,而不是放在头文件中。这样,“私有”符号在内部可用,但对外部调用者不可用。在一个完整的例子中:
core.h
:
void my_public_fn();
core_priv.h
:
void my_private_fn();
core.c
:
#include <stdio.h>
#include "core.h"
#include "core_priv.h"
void my_private_fn() {
printf("Private function called.\n");
}
void my_public_fn() {
printf("Public function called.\n");
}
module_a.h
:
#include "core.h"
void module_a_fn();
module_a.c
:
#include "core_priv.h"
#include "module_a.h"
void module_a_fn() {
my_private_fn();
my_public_fn();
}
如果需要,我们可以将多个模块分组到一个公共库头中。
library.h
:
#include "module_a.h"
// etc.
这样一来,使用该库的程序只需包含一个文件:
main.c
:
#include "library.h"
int main() {
//my_private_fn(); // This triggers a compile warning.
my_public_fn(); // I can still reach the "core" public function.
module_a_fn(); // This calls the "private" function internally.
return 0;
}
用 gcc -Wall *.c -o main.o
编译并执行 ./main.o
产生:
Public function called.
Private function called.
Public function called.
我正在编写一个分布在多个文件中的 C99 库,例如
// core.h
void my_private_fn();
void API_my_public_fn();
// core.c
#include "core.h"
void my_private_fn() {
do_something();
}
void API_my_public_fn() {
do_something_else();
}
// module_a.h
#include "core.h"
void API_useful_thing();
// module_a.c
#include "module_a.h"
void API_useful_thing() {
my_private_fn();
}
我只希望 API_
前缀函数对使用该库的程序可见,但我还需要在 core.h
中公开 my_private_fn
以便 module_a.c
。在 C 中有没有办法让 my_private_fn
只在库中可见?
将它们放在仅在库内部使用而不分发给最终用户的内部头文件中——比如说,core_internal.h
。
如果函数必须只在定义它的编译单元中可见,那么您可以声明它static
。因为 C 语言提供的范围很少:一个符号只能有 3 个范围:
- 局部于块(块可以是函数或函数内的块)
- 静态作用域(函数外的静态声明):符号只在声明它的编译单元可见
- 全局范围(函数外的非静态声明):符号在整个程序中可见。
您最多可以隐藏 私有包含文件中您未在官方文档中声明的声明API。那样听话的用户不应该使用它。但是你不能阻止用户将声明放在自己的代码中并使用该函数。
我找到了一种更简洁的方法来根据我选择的 Serge 的答案来布置我的代码,它的最大优点是。
关键是将“私有”函数放在只包含在 C 文件中的头文件中,而不是放在头文件中。这样,“私有”符号在内部可用,但对外部调用者不可用。在一个完整的例子中:
core.h
:
void my_public_fn();
core_priv.h
:
void my_private_fn();
core.c
:
#include <stdio.h>
#include "core.h"
#include "core_priv.h"
void my_private_fn() {
printf("Private function called.\n");
}
void my_public_fn() {
printf("Public function called.\n");
}
module_a.h
:
#include "core.h"
void module_a_fn();
module_a.c
:
#include "core_priv.h"
#include "module_a.h"
void module_a_fn() {
my_private_fn();
my_public_fn();
}
如果需要,我们可以将多个模块分组到一个公共库头中。
library.h
:
#include "module_a.h"
// etc.
这样一来,使用该库的程序只需包含一个文件:
main.c
:
#include "library.h"
int main() {
//my_private_fn(); // This triggers a compile warning.
my_public_fn(); // I can still reach the "core" public function.
module_a_fn(); // This calls the "private" function internally.
return 0;
}
用 gcc -Wall *.c -o main.o
编译并执行 ./main.o
产生:
Public function called.
Private function called.
Public function called.