0

使用 off_t 作为一个函数 (seek) 参数的库。库和应用程序的编译方式不同,一种关闭了大文件支持,另一种关闭了大文件支持。这种情况会导致奇怪的运行时错误,因为两者对 off_t 的解释不同。库如何在运行时检查应用程序的 off_t 大小?或者是否有另一种解决方案,以便至少用户得到一个有意义的错误?

编辑:该库(用 c 和 autoconf 编程)已经存在,并且一些第三方应用程序使用它。该库可以使用大文件支持进行编译(默认情况下通过 AC_SYS_LARGEFILE)。它是多平台的,不仅仅是linux。如何检测/防止已安装的应用程序会因 LFS 的变化而损坏?

4

3 回答 3

2

You could add an API to the library to return the sizeof(off_t) and then check it from the client. Alternatively the library could require every app to provide the API in order to successfully link:

library.c:

size_t lib_get_off_t_size (void)
{
    return (sizeof(off_t));
}

client.c (init_function):

if (lib_get_off_t_size() != sizeof(off_t) {
    printf("Oh no!\n");
    exit();
}

If the library has an init function then you could put the check there, but then the client would have to supply the API to get the size of its off_t, which generally isn't how libraries work.

于 2008-10-29T20:33:10.553 回答
1

在 Linux 上,当在打开大文件支持的情况下编译库时,off_t定义为与off64_t. 因此,如果该库是使用大文件支持编译的库,您可以将其接口更改为始终使用off64_t而不是off_t(这可能需要_LARGEFILE64_SOURCE)并完全避免该问题。

您还可以检查应用程序是否正在使用大文件支持进行编译(通过查看是否_FILE_OFFSET_BITS未定义或),如果编译方式错误,则32拒绝编译(使用);#error请参阅/usr/include/features.h功能测试宏

于 2008-10-29T21:42:47.557 回答
0

As said before, the library will not be able to know how the application (being client to the library) is compiled, but the other way round has to work. Besides, I think you are talking about dynamic linking, since static linking certainly would not have different switches at same build time.

Similar to the already given answer by "Andrew Johnson", the library could provide a method for finding out whether it was compiled with large file support or not. Knowing that such build-time switches are mostly done with defines in C, this could be looking like this:

//in library:
BOOL isLargeFileSupport (void)
{
#ifdef LARGE_FILE_SUPPORT
    return TRUE;
#else
    return FALSE;
#endif
}

The application then knows how to handle file sizes reported by that lib, or can refuse to work when incompatible:

//in application
BOOL bLibLFS = lib_isLargeFileSupport();
BOOL bAppLFS = FALSE;
#ifdef LARGE_FILE_SUPPORT
    bAppLFS = TRUE;
#endif

if (bLibLFS != bAppLFS)
    //incompatible versions, bail out
    exit(0);
于 2008-10-29T21:07:56.083 回答