gpt4 book ai didi

c - 如何在 c 中隐藏公共(public) API 的辅助函数

转载 作者:太空狗 更新时间:2023-10-29 15:01:28 25 4
gpt4 key购买 nike

我正在做一个项目,我需要创建一个 API。我正在使用套接字在服务器(我的应用程序)和客户端(使用我的 API 的其他应用程序)之间进行通信。

这个项目是用c而不是C++

我有 Linux 背景,这是我使用 Windows、Visual Studio 2008 和 dll 库的第一个项目。

我在客户端和服务器之间进行通信,但我有一些在两个项目中都是重复的。我想创建一个库(可能是一个 dll 文件),两个项目都可以链接到它,所以我不必维护额外的代码。

我还必须创建一个库,其中包含我需要为我的客户提供的 API。在我想要公开的 API 函数中是对这些辅助函数的调用,它们是“重复代码”,我不想将这些函数公开给我的客户端,但我确实希望我的服务器能够使用这些函数。我该怎么做?

我会试着用一个例子来说明。这就是我的出发点。

服务器项目:

int Server_GetPacket(SOCKET sd);
int ReceiveAll(SOCKET sd, char *buf, int len);
int VerifyLen(char *buf);

客户项目:

int Client_SendCommand(int command);
int Client_GetData(int command, char *buf, int len);
int ReceiveAll(SOCKET sd, char *buf, int len);
int VerifyLen(char *buf);

这就是我想要的结果:

//Server Project:
int Server_GetPacket(SOCKET sd);

// library with public and private types
// private API (not exposed to my client)
int ReceiveAll(SOCKET sd, char *buf, int len);
int VerifyLen(char *buf);
// public API (header file available for client)
int Client_SendCommand(int command);
int Client_GetData(int command, char *buf, int len);

感谢任何帮助。

最佳答案

使用

static int ReceiveAll(SOCKET sd, char *buf, int len);

等等,使函数成为文件/编译单元的本地函数。

关于c - 如何在 c 中隐藏公共(public) API 的辅助函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3016526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com