FILE* 的二进制兼容性

发布于 2024-10-31 10:45:28 字数 432 浏览 4 评论 0原文

我正在设计 C 库,它可以进行一些数学计算。我需要指定序列化接口才能保存然后加载一些数据。问题是,在库的公共 API 中使用 FILE* 指针是否正确(从二进制兼容性的角度来看)?

目标平台是:

  • Linux x86, x86_64 with gcc >= 3.4.6
  • Windows x86, x86_64 >= WinXP with VS >= 2008sp1

我需要尽可能多的二进制兼容,所以目前我的变体是以下:

void SMModuleSave(SMModule* module, FILE* dest);
SMModule* SMModuleLoad(FILE* src);

所以我很好奇使用 FILE* 是否正确或更好地切换到 wchar*/char* ?

I am designing C library which does some mathematical calculations. I need to specify serialization interface to be able to save and then load some data. The question is, is it correct (from binary compatibility point of view) to use FILE* pointer in the public API of library?

Target platfoms are:

  • Linux x86, x86_64 with gcc >= 3.4.6
  • Windows x86, x86_64 >= WinXP with VS >= 2008sp1

I need to be as much binary compatible as it possible, so at the moment my variant is the following:

void SMModuleSave(SMModule* module, FILE* dest);
SMModule* SMModuleLoad(FILE* src);

So I am curious if it is correct to use FILE* or better switch to wchar*/char* ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巾帼英雄 2024-11-07 10:45:28

我不同意 ThiefMaster 的观点:当存在等效的便携式解决方案。

我可能会使用 FILE * ,而不是从库中按名称打开文件:这对于库用户来说可能会更麻烦,但它也更灵活,因为大多数 libc 实现都提供了各种方式用于打开文件(fopen()_wfopen()_fdopen()fdopen()fmemopen(),...) 并且您不必自己维护单独的宽字符 API。

I don't agree with ThiefMaster: there's no benefit in going native (ie using file descriptors of type int on linux and handles of type void * on windows) when there's an equivalent portable solution.

I'd probably go with FILE * instead of opening the files by name from within the library: It might be more of a hassle for library users, but it's also more flexible as most libc implementations provide various ways for file opening (fopen(), _wfopen(), _fdopen(), fdopen(), fmemopen(),...) and you don't have to maintain seperate wide-char APIs yourself.

山川志 2024-11-07 10:45:28

我不会使用任何一个,而是让用户将文件描述符作为 int 传递。

然后您可以在代码中使用fdopen()来获取FILE*

然而,当使用 Windows 时,即使它确实有一些辅助函数来获取数字文件描述符,它也可能不是最好的解决方案。


但是,传递 FILE*const char* 也应该没问题。我更喜欢传递文件名,因为如果库负责打开/关闭文件,则需要编写的代码更少。

I'd use neither but let the user pass a file descriptor as an int.

Then you can fdopen() it in your code to get a FILE*.

However, when using windows it might not be the best solution even though it does have some helper functions to get a numeric file descriptor.


However, passing a FILE* or a const char* should be fine, too. I'd prefer passing a filename as it's less code to write if a library takes care of opening/closing a file.

短叹 2024-11-07 10:45:28

是的,从稳定的二进制接口的角度来看,在这里使用 FILE * 是正确的。我想您可能会将其与使用 FILE 而不是指向它的指针混淆。请注意,标准库的 fopenfgets 等函数都使用(作为参数和返回值)FILE * 类型作为他们的公共接口。

Yes it is correct, from a stable binary interface perspective, to use FILE * here. I think perhaps you're confusing this with using FILE as opposed to a pointer to it. Notice that your standard library's fopen, fgets, etc. functions all use (both as arguments and return values) the FILE * type as part of their public interfaces.

太阳公公是暖光 2024-11-07 10:45:28

FILE * 是标准 ANSI/ISO C89 和 C99(甚至 K&R)类型。这是一个便携性的梦想,我更喜欢它。你可以安全地接受它。没有比这更好的了。

A FILE * is a standard ANSI/ISO C89 and C99 (even K&R) type. It is a portability dream and I'd prefer it over anything else. You're safe to go with it. It won't get any better than that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文