gpt4 book ai didi

c - 如何可移植地打开大文件支持?

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:20 26 4
gpt4 key购买 nike

我目前正在编写一个 C 程序,用于读取和写入大小可能超过 2 GiB 的文件。在 linux 上 feature_test_macros(7) 指定:

   _LARGEFILE64_SOURCE
Expose definitions for the alternative API specified by the LFS (Large File Summit) as a "tran‐
sitional extension" to the Single UNIX Specification. (See ⟨http://opengroup.org/platform
/lfs.html⟩) The alternative API consists of a set of new objects (i.e., functions and types)
whose names are suffixed with "64" (e.g., off64_t versus off_t, lseek64() versus lseek(), etc.).
New programs should not employ this interface; instead _FILE_OFFSET_BITS=64 should be employed.

_FILE_OFFSET_BITS
Defining this macro with the value 64 automatically converts references to 32-bit functions and
data types related to file I/O and file system operations into references to their 64-bit coun‐
terparts. This is useful for performing I/O on large files (> 2 Gigabytes) on 32-bit systems.
(Defining this macro permits correctly written programs to use large files with only a recompi‐
lation being required.) 64-bit systems naturally permit file sizes greater than 2 Gigabytes,
and on those systems this macro has no effect.

这能保证便携吗?

Solaris 在 lfcompile(5) 中建议:

 Applications can be compiled in the large  file  compilation
environment by using the following methods:

o Use the getconf(1) utility with one or more of the
arguments listed in the table below. This method is
recommended for portable applications.

____________________________________________________________
| argument | purpose |
|__________________|________________________________________|
| LFS_CFLAGS | obtain compilation flags necessary to|
| | enable the large file compilation|
| | environment |
| LFS_LDFLAGS | obtain link editor options |
| LFS_LIBS | obtain link library names |
| LFS_LINTFLAGS | obtain lint options |
|__________________|________________________________________|

o Set the compile-time flag _FILE_OFFSET_BITS to 64
before including any headers. Applications may com-
bine objects produced in the large file compilation
environment with objects produced in the transi-
tional compilation environment, but must be careful
with respect to interoperability between those
objects. Applications should not declare global
variables of types whose sizes change between com-
pilation environments.

这是可移植的吗?在我的 linux 机器上,将这些 key 传递给 getconf 将不会输出任何内容。 (这意味着 key 存在)。

有没有可移植的方法?

最佳答案

使用 confstr interface或对应的getconf program并尝试各种 _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS 等选项,直到找到一个可行的选项。这将为您提供适当环境所需的 CFLAGSLDFLAGSLIBS

关于c - 如何可移植地打开大文件支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18293310/

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