- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一个数学库,稍后我想将其作为我的渲染管道的一部分。
编辑我想坚持使用 C99。
我计划编写同一个库的 3 个版本,一个通用的标量版本,一个用于移动平台的 neon 版本,以及一个用于支持它的平台的 SSE 实现。
我想知道如何构建我的文件以具有一个接口(interface),但取决于编译器标志有不同的实现。
为简单起见,我将使用基本的数学函数。
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);
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);
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 文件中实现。
现在我将有一个类 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
来隐藏底层函数的所有实现,这样一旦我包含了头文件,就不需要再弄乱底层文件了.
然后可以在编译时创建标量、sse 或 neon 构建类型,并且此库的用户永远不必担心在他们的文件中添加#ifdefs。
这个 basic_math.h
模块必须是所有 #ifdef
所在的地方,但如果您使用的文件在你的 main.c
。
此外,如果我忽略了任何内容,我也希望得到反馈。
最佳答案
一个明显的问题是在 C 的单个程序中只能有一个具有任何给定名称的函数(在没有动态加载和通过函数指针访问的情况下);没有函数重载。在存在函数重载的 C++ 中,您想要做的事情可能会更容易。
basic_math.h
中函数的返回类型是意外的:
basic_math_struct add(basic_math_struct* out, const int* in_a, int* in_b);
在其他 header 中,add
的返回类型是int
。不清楚为什么 in_a
参数是 const int *
而 in_b
是非常量 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
代码那样返回 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
头文件,然后适当定义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…
}
只要在程序的同一次运行中没有任何构建需要具有多种可用类型,这就很好而且很直接。然后你会遇到问题,但这还不是你所描述的。
在带有标签 c 的 SO 上查找“不透明类型” (因此在 SO 搜索栏中输入 [c] opaque type
)。您应该会找到许多相关的问题以及合适的答案。
关于C 头文件和源文件结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33468757/
我有一个项目,其中包含两个源文件和一个头文件(其中包含名为 Get.c 的源文件之一中定义的函数的原型(prototype)),问题是: 当我在另一个源文件中包含 Get.c 时,它给出了错误 错误:
有没有尽可能多的语法结构的java源代码文件? java 编译器的某种测试文件。我需要该文件来测试 java 源代码解析器。 最佳答案 看看 Java Compatibility Kit ,特别Tec
尝试在命令提示符下编译时,我在使用 codeblocks-16.01mingw-setup.exe - 安装在路径不包含空格的文件中时遇到问题。初学者指南建议在命令提示符中使用以下行: gcc car
我编写了一个使用 Cryptopp 库的程序。我已经创建了静态库并将其包含在我的分发版中,但是该程序还需要很多头文件。我的问题是我需要在我的发行版中包含所有这些头文件吗? 例如,我编写了一个代码,其中
我在源代码树中有 protobuf 原型(prototype)文件。我想在每次更改和首次运行时从原型(prototype)文件生成源文件(例如,我创建新的原型(prototype)文件)。然后,我想从
我知道在 Java 中加载文件而不指定要使用的编码是平台相关的。但我的问题是关于 .java 源文件本身中包含的文本:用于这些文件的编码是否仍然相关一旦编译? 例如,如果我在 Windows 上有一个
编辑:{ 我想我在这里添加了很多(太多)信息(阅读)。我想要的是: 我正在使用一个网站(没有.csproj文件) 我需要多个源代码文件才能运行我的aspx 据我所知,当请求aspx文件时,我要么需要使
我从 Asio 的示例页面中获取了以下代码 class tcp_connection : public boost::enable_shared_from_this { public: t
我已经使用 VS2010 为我的 C++ 应用程序创建了一个 Windows 安装部署。但是我的问题是我不知道如何将我自己的一些代码压缩到安装向导中(这可能吗?)。问题是在安装过程中我想要: 要求用户
最近我创建了一个 bash 脚本,我应该在 cron 中运行它。 准备好bash脚本并正常运行后,我将其放入Cron中,发现它失败了。作为第二步,我删除了所有环境依赖项,即我指定的不是 file.tx
考虑以下场景 例子.txt: ÄäÖöÜü Java 源代码: try (FileInputStream fileInputStream = new FileInputStream("example.
背景我主要使用嵌入式 C/C++ 进行编程,但有时我必须为我们的 API 进行一些 C# 编程。为此,我使用 Visual Studio 2017 为我们的客户创建 API DLL。 C# API 和
这个问题在这里已经有了答案: ggplot's qplot does not execute on sourcing (1 个回答) 关闭 9 年前。 考虑这种形式的源文件: # initializ
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我需要读取一个 Racket 源文件并通过宏扩展来运行它。我有一个简单的测试文件,Racket 本身很乐意接受: C:\ayane>type factorial.rkt #lang racket (p
我的源文件位于此文件夹中:c:\data\mycompany。我的所有源文件的第一行都包含以下内容:package mycompany;现在,我从 c:\data 文件夹中使用以下命令编译了所有内容:
这个问题在这里已经有了答案: #include all .cpp files into a single compilation unit? (6 个回答) The benefits / disadv
我对这个 JAVA RMI 项目真的很陌生。我只是想知道我可以使用什么编译器来编译我的JAVA源代码?以及如何做到这一点? 我正在尝试编译来自 http://www.eg.bucknell.edu/~
我想使用 Subversion 并仅 check out 源文件(例如:仅 check out .c 、 .cpp 和 .h 文件)。这可能吗 ?如果是这样,我该怎么做? 我正在尝试从以下位置获取 w
有没有办法使用指向唯一头文件的不同配置(或不同方法)来设置单个项目。 我想创建一个包含两个头文件的 C 项目(例如 header1.h 和 header2.h)。在我的 .c 源文件中,我定义了两个配
我是一名优秀的程序员,十分优秀!