C header 和源文件结构
C header and source files structure
我正在编写一个数学库,稍后我想将其作为我的渲染管道的一部分。
编辑
我想坚持使用 C99。
我计划编写同一个库的 3 个版本,一个通用的标量版本,一个用于移动平台的 neon 版本和一个用于支持它的平台的 SSE 实现。
我想知道如何构建我的文件以具有一个接口,但取决于编译器标志有不同的实现。
为简单起见,我将使用基本的数学函数。
basic_math_scalar
Header basic_math_scalar.h
typedef struct scalar_struct {
...
...
};
int add(scalar_struct* out, const int* in_a, int* in_b);
int sub(scalar_struct* out, const int* in_a, int* in_b);
int mult(scalar_struct* out, const int* in_a, int* in_b);
int div(scalar_struct* out, const int* in_a, int* in_b);
basic_math_sse
Header basic_math_sse.h
typedef struct sse_struct {
...
...
};
int add(sse_struct* out, const int* in_a, int* in_b);
int sub(sse_struct* out, const int* in_a, int* in_b);
int mult(sse_struct* out, const int* in_a, int* in_b);
int div(sse_struct* out, const int* in_a, int* in_b);
basic_math_neon
Header basic_math_neon.h
typedef struct neon_struct {
...
...
};
int add(neon_struct* out, const int* in_a, int* in_b);
int sub(neon_struct* out, const int* in_a, int* in_b);
int mult(neon_struct* out, const int* in_a, int* in_b);
int div(neon_struct* out, const int* in_a, int* in_b);
以上每个文件都将在 .c 文件中实现。
现在我将有一个 class main.c
来实现这些并根据编译器标志进行切换。
main.c
#include <stdlib.h>
#include <stdio.h>
#include "basic_math.h"
int main(int argc, char** argv) {
basic_math_struct* a = create_bms();
a = add(1, 2);
printf("value of a: %d\n",bms_value(a)); //should equal 3
cleanup_bms(a);
return 0;
}
上面的代码中少了一大块。我有实际的数学函数,然后我有使用这些数学函数的 main.c,但是有一个中间步骤我在设计时遇到了问题,basic_math.h
假设 basic_math.h 看起来像这样。
#ifdef SSE
#include "basic_math_sse.h"
typedef sse_struct basic_math_struct;
#elif NEON
#include "basic_math_neon.h"
typedef neon_struct basic_math_struct;
#else
#include "basic_math_scalar.h"
typedef scalar_struct basic_math_struct;
#endif
basic_math_struct add(basic_math_struct* out, const int* in_a, int* in_b);
basic_math_struct sub(basic_math_struct* out, const int* in_a, int* in_b);
basic_math_struct mult(basic_math_struct* out, const int* in_a, int* in_b);
basic_math_struct div(basic_math_struct* out, const int* in_a, int* in_b);
基本上我想用basic_math.h
文件来隐藏底层函数的所有实现,这样一旦我包含了header文件,就不需要再乱搞底层文件了再也没有了。
然后可以在编译时生成标量、sse 或 neon 构建类型,并且此库的用户永远不必担心在他们的文件中添加 #ifdefs。
此 basic_math.h
模块必须是所有 #ifdef
所在的位置,但如果您正在使用 main.c
中的文件,则不必担心。
此外,如果我忽略了任何内容,我也希望得到反馈。
一个明显的问题是,在 C 的单个程序中,您只能拥有一个具有任何给定名称的函数(在没有动态加载和通过函数指针访问的情况下);没有函数重载。在存在函数重载的 C++ 中,您想要做的事情可能会更容易。
basic_math.h
中函数的 return 类型是意外的:
basic_math_struct add(basic_math_struct* out, const int* in_a, int* in_b);
在其他header中,add
的return类型是int
。不清楚为什么 in_a
参数是 const int *
而 in_b
是 non-constant int *
.
调用代码是否会通过内存分配函数实例化其中一个结构?也就是说,调用代码可以写成:
basic_math_struct x;
或者总是使用指针的约束:
basic_math_struct *px = bms_create();
或多或少如您的代码所示?请注意,使用 bms_
作为前缀比 _bms
作为后缀更常规,尽管使用后缀并非完全站不住脚。
请注意,您的示例 main()
包括:
basic_math_struct* a = create_bms();
a = add(1, 2);
这不是对问题中任何 add
函数的调用。从表面上看,您可能打算:
*a = add(a, 1, 2);
这将与您的 header 中的 add
函数相匹配,但这是非常可疑的代码。如果像 scalar
、sse
和 neon
代码中的函数 returned int
会更有意义:
if (add(a, 1, 2) != 0)
…report BMS error…
如果调用代码永远不会分配结构(它只会包含指向结构的指针)并且它永远不会尝试取消引用指针以获取结构中的元素(它只会使用 API 调用以获取数据),那么您可以避免将类型的内部结构完全暴露给调用代码,而只需使用 不透明类型 .
不透明类型
basic_math.h
typedef struct basic_math_struct basic_math_struct;
int bms_add(basic_math_struct *out, const int *in_a, int *in_b);
int bms_sub(basic_math_struct *out, const int *in_a, int *in_b);
int bms_mul(basic_math_struct *out, const int *in_a, int *in_b);
int bms_div(basic_math_struct *out, const int *in_a, int *in_b);
basic_math_struct *bms_create(void);
请注意,这明确 不 包括任何特定的 header。
用户可以相应地编写代码:
basic_math_struct *a = bms_create();
if (bms_add(a, 1, 2) != 0)
…deal with BMS error…
三个系统的实现代码都会包含basic_math.h
header,然后适当定义struct basic_math_struct
:
#include "basic_math.h"
struct basic_math_struct
{
...
...
};
并将相应地实施代码:
int bms_add(basic_math_struct *out, const int *in_a, int *in_b)
{
…implementation for scalar, or neon, or sse…
}
这很好,而且 straight-forward 只要没有任何构建需要在同一个 运行 程序中使用不止一种可用的类型。那你就会遇到问题,但这还不是你所描述的。
在带有标签 c 的 SO 上查找 'opaque types'(因此在 SO 搜索栏中输入 [c] opaque type
)。您应该会找到许多相关的问题和不错的答案。
我正在编写一个数学库,稍后我想将其作为我的渲染管道的一部分。
编辑 我想坚持使用 C99。
我计划编写同一个库的 3 个版本,一个通用的标量版本,一个用于移动平台的 neon 版本和一个用于支持它的平台的 SSE 实现。
我想知道如何构建我的文件以具有一个接口,但取决于编译器标志有不同的实现。
为简单起见,我将使用基本的数学函数。
basic_math_scalar
Header basic_math_scalar.h
typedef struct scalar_struct {
...
...
};
int add(scalar_struct* out, const int* in_a, int* in_b);
int sub(scalar_struct* out, const int* in_a, int* in_b);
int mult(scalar_struct* out, const int* in_a, int* in_b);
int div(scalar_struct* out, const int* in_a, int* in_b);
basic_math_sse
Header basic_math_sse.h
typedef struct sse_struct {
...
...
};
int add(sse_struct* out, const int* in_a, int* in_b);
int sub(sse_struct* out, const int* in_a, int* in_b);
int mult(sse_struct* out, const int* in_a, int* in_b);
int div(sse_struct* out, const int* in_a, int* in_b);
basic_math_neon
Header basic_math_neon.h
typedef struct neon_struct {
...
...
};
int add(neon_struct* out, const int* in_a, int* in_b);
int sub(neon_struct* out, const int* in_a, int* in_b);
int mult(neon_struct* out, const int* in_a, int* in_b);
int div(neon_struct* out, const int* in_a, int* in_b);
以上每个文件都将在 .c 文件中实现。
现在我将有一个 class main.c
来实现这些并根据编译器标志进行切换。
#include <stdlib.h>
#include <stdio.h>
#include "basic_math.h"
int main(int argc, char** argv) {
basic_math_struct* a = create_bms();
a = add(1, 2);
printf("value of a: %d\n",bms_value(a)); //should equal 3
cleanup_bms(a);
return 0;
}
上面的代码中少了一大块。我有实际的数学函数,然后我有使用这些数学函数的 main.c,但是有一个中间步骤我在设计时遇到了问题,basic_math.h
假设 basic_math.h 看起来像这样。
#ifdef SSE
#include "basic_math_sse.h"
typedef sse_struct basic_math_struct;
#elif NEON
#include "basic_math_neon.h"
typedef neon_struct basic_math_struct;
#else
#include "basic_math_scalar.h"
typedef scalar_struct basic_math_struct;
#endif
basic_math_struct add(basic_math_struct* out, const int* in_a, int* in_b);
basic_math_struct sub(basic_math_struct* out, const int* in_a, int* in_b);
basic_math_struct mult(basic_math_struct* out, const int* in_a, int* in_b);
basic_math_struct div(basic_math_struct* out, const int* in_a, int* in_b);
基本上我想用basic_math.h
文件来隐藏底层函数的所有实现,这样一旦我包含了header文件,就不需要再乱搞底层文件了再也没有了。
然后可以在编译时生成标量、sse 或 neon 构建类型,并且此库的用户永远不必担心在他们的文件中添加 #ifdefs。
此 basic_math.h
模块必须是所有 #ifdef
所在的位置,但如果您正在使用 main.c
中的文件,则不必担心。
此外,如果我忽略了任何内容,我也希望得到反馈。
一个明显的问题是,在 C 的单个程序中,您只能拥有一个具有任何给定名称的函数(在没有动态加载和通过函数指针访问的情况下);没有函数重载。在存在函数重载的 C++ 中,您想要做的事情可能会更容易。
basic_math.h
中函数的 return 类型是意外的:
basic_math_struct add(basic_math_struct* out, const int* in_a, int* in_b);
在其他header中,add
的return类型是int
。不清楚为什么 in_a
参数是 const int *
而 in_b
是 non-constant int *
.
调用代码是否会通过内存分配函数实例化其中一个结构?也就是说,调用代码可以写成:
basic_math_struct x;
或者总是使用指针的约束:
basic_math_struct *px = bms_create();
或多或少如您的代码所示?请注意,使用 bms_
作为前缀比 _bms
作为后缀更常规,尽管使用后缀并非完全站不住脚。
请注意,您的示例 main()
包括:
basic_math_struct* a = create_bms();
a = add(1, 2);
这不是对问题中任何 add
函数的调用。从表面上看,您可能打算:
*a = add(a, 1, 2);
这将与您的 header 中的 add
函数相匹配,但这是非常可疑的代码。如果像 scalar
、sse
和 neon
代码中的函数 returned int
会更有意义:
if (add(a, 1, 2) != 0)
…report BMS error…
如果调用代码永远不会分配结构(它只会包含指向结构的指针)并且它永远不会尝试取消引用指针以获取结构中的元素(它只会使用 API 调用以获取数据),那么您可以避免将类型的内部结构完全暴露给调用代码,而只需使用 不透明类型 .
不透明类型
basic_math.h
typedef struct basic_math_struct basic_math_struct;
int bms_add(basic_math_struct *out, const int *in_a, int *in_b);
int bms_sub(basic_math_struct *out, const int *in_a, int *in_b);
int bms_mul(basic_math_struct *out, const int *in_a, int *in_b);
int bms_div(basic_math_struct *out, const int *in_a, int *in_b);
basic_math_struct *bms_create(void);
请注意,这明确 不 包括任何特定的 header。
用户可以相应地编写代码:
basic_math_struct *a = bms_create();
if (bms_add(a, 1, 2) != 0)
…deal with BMS error…
三个系统的实现代码都会包含basic_math.h
header,然后适当定义struct basic_math_struct
:
#include "basic_math.h"
struct basic_math_struct
{
...
...
};
并将相应地实施代码:
int bms_add(basic_math_struct *out, const int *in_a, int *in_b)
{
…implementation for scalar, or neon, or sse…
}
这很好,而且 straight-forward 只要没有任何构建需要在同一个 运行 程序中使用不止一种可用的类型。那你就会遇到问题,但这还不是你所描述的。
在带有标签 c 的 SO 上查找 'opaque types'(因此在 SO 搜索栏中输入 [c] opaque type
)。您应该会找到许多相关的问题和不错的答案。