rtissera / libchdr

Standalone library for reading MAME's CHDv1-v5 formats.
BSD 3-Clause "New" or "Revised" License
98 stars 42 forks source link

Compilation Error #92

Open smileaf419 opened 1 year ago

smileaf419 commented 1 year ago

Using: cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release -DWITH_SYSTEM_ZLIB=1 -DCMAKE_C_COMPILER=clang -D CMAKE_C_STANDARD=11 -D CMAKE_C_EXTENSIONS=0

-- The C compiler identification is Clang 16.0.2 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/clang - skipped -- Detecting C compile features -- Detecting C compile features - done -- Found ZLIB: /lib/libz.so (found version "1.2.13")
-- Configuring done (0.2s) -- Generating done (0.0s) -- Build files have been written to: /var/tmp/spkg/libchdr-master/build [ 4%] Building C object deps/lzma-22.01/CMakeFiles/lzma.dir/src/Alloc.c.o [ 8%] Building C object deps/lzma-22.01/CMakeFiles/lzma.dir/src/Bra86.c.o [ 12%] Building C object deps/lzma-22.01/CMakeFiles/lzma.dir/src/BraIA64.c.o [ 16%] Building C object deps/lzma-22.01/CMakeFiles/lzma.dir/src/CpuArch.c.o [ 20%] Building C object deps/lzma-22.01/CMakeFiles/lzma.dir/src/Delta.c.o [ 24%] Building C object deps/lzma-22.01/CMakeFiles/lzma.dir/src/LzFind.c.o [ 28%] Building C object deps/lzma-22.01/CMakeFiles/lzma.dir/src/Lzma86Dec.c.o [ 32%] Building C object deps/lzma-22.01/CMakeFiles/lzma.dir/src/LzmaDec.c.o [ 36%] Building C object deps/lzma-22.01/CMakeFiles/lzma.dir/src/LzmaEnc.c.o [ 40%] Building C object deps/lzma-22.01/CMakeFiles/lzma.dir/src/Sort.c.o [ 44%] Linking C static library liblzma.a [ 44%] Built target lzma [ 48%] Building C object CMakeFiles/chdr-static.dir/src/libchdr_bitstream.c.o [ 52%] Building C object CMakeFiles/chdr-static.dir/src/libchdr_cdrom.c.o [ 56%] Building C object CMakeFiles/chdr-static.dir/src/libchdr_chd.c.o /var/tmp/spkg/libchdr-master/src/libchdr_chd.c:814:10: warning: implicit conversion from enumeration type 'enum huffman_error' to different enumeration type 'chd_error' (aka 'enum _chd_error') [-Wenum-conversion] return err;


/var/tmp/spkg/libchdr-master/src/libchdr_chd.c:2993:6: error: call to undeclared function 'ftello'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        p = core_stdio_ftell_impl(fp);
            ^
/var/tmp/spkg/libchdr-master/src/libchdr_chd.c:2987:32: note: expanded from macro 'core_stdio_ftell_impl'
        #define core_stdio_ftell_impl ftello
                                      ^
/var/tmp/spkg/libchdr-master/src/libchdr_chd.c:2993:6: note: did you mean 'ftell'?
/var/tmp/spkg/libchdr-master/src/libchdr_chd.c:2987:32: note: expanded from macro 'core_stdio_ftell_impl'
        #define core_stdio_ftell_impl ftello
                                      ^
/usr/include/stdio.h:718:17: note: 'ftell' declared here
extern long int ftell (FILE *__stream) __wur;
                ^
/var/tmp/spkg/libchdr-master/src/libchdr_chd.c:2994:2: error: call to undeclared function 'fseeko'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        core_stdio_fseek_impl(fp, 0, SEEK_END);
        ^
/var/tmp/spkg/libchdr-master/src/libchdr_chd.c:2986:32: note: expanded from macro 'core_stdio_fseek_impl'
        #define core_stdio_fseek_impl fseeko
                                      ^
/var/tmp/spkg/libchdr-master/src/libchdr_chd.c:2994:2: note: did you mean 'fseek'?
/var/tmp/spkg/libchdr-master/src/libchdr_chd.c:2986:32: note: expanded from macro 'core_stdio_fseek_impl'
        #define core_stdio_fseek_impl fseeko
                                      ^
/usr/include/stdio.h:713:12: note: 'fseek' declared here
extern int fseek (FILE *__stream, long int __off, int __whence);
           ^
/var/tmp/spkg/libchdr-master/src/libchdr_chd.c:3031:9: error: call to undeclared function 'fseeko'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        return core_stdio_fseek_impl((FILE*)file->argp, offset, whence);
               ^
/var/tmp/spkg/libchdr-master/src/libchdr_chd.c:2986:32: note: expanded from macro 'core_stdio_fseek_impl'
        #define core_stdio_fseek_impl fseeko
                                      ^
1 warning and 3 errors generated.
make[2]: *** [CMakeFiles/chdr-static.dir/build.make:104: CMakeFiles/chdr-static.dir/src/libchdr_chd.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:118: CMakeFiles/chdr-static.dir/all] Error 2
make: *** [Makefile:136: all] Error 2
rtissera commented 1 year ago

Can you share more on your building environment ? Thanks :)

smileaf419 commented 1 year ago

I run a self-built LFS-referenced variant. (Sort of experimental toy: https://github.com/smileaf419/spkg) The error was originally found building ares. No cflags are set (an output of ENV can also be found within the bug report there)

rtissera commented 1 year ago

Ok, let me try to reproduce and come back to you. Obviously sounds like fseeko/ftello are not detected for some reason.

invertego commented 1 year ago

To provide more context, ares currently builds all C source files, including those from libchdr, with -std=c11. This causes __STRICT_ANSI__ to be defined, and as a result glibc does not define certain POSIX APIs that it will otherwise define if you specify no standard or one of the GNU variants (e.g. -std=gnu11). fseeko/ftello are POSIX APIs, not standard C.

There are many macros supported by glibc that can influence these definitions:

References: https://man7.org/linux/man-pages/man7/feature_test_macros.7.html https://github.com/bminor/glibc/blob/master/include/features.h

For ares, we can work around this by tweaking the flags passed to the compiler, but there's an opportunity to make the following code more robust across varying compilation environments.

https://github.com/rtissera/libchdr/blob/fec8ab94212cc65d9d9a62cb3da924f5830c04b0/src/libchdr_chd.c#L2973-L2988

There are a couple of issues with this code as it stands:

There are a lot of options for which macros to define, of course. One or more of _POSIX_C_SOURCE/_XOPEN_SOURCE/_GNU_SOURCE/_LARGEFILE_SOURCE combined with _FILE_OFFSET_BITS should cover the bases, though. For what it's worth, the GNU libc manual recommends _GNU_SOURCE, though it is by far the most expansive.

As an aside, none of this seems to be an issue with BSD libc. It defines fseeko/ftello even with -std=c11 and the offsets are always 64-bit. I have not examined other C standard library implementations.