cargo-zigbuild icon indicating copy to clipboard operation
cargo-zigbuild copied to clipboard

Getting compiler errors about a missing __COLD attribute? from zig headers

Open weiznich opened this issue 1 year ago • 5 comments
trafficstars

I have a project that depends on netcdf-src 0.4.0. Trying to build that in the official rust-lang docker container with cargo-zigbuild 0.19.1 and zig 0.13.0 installed fails with the following error messages:

/opt/zig/lib/libc/include/generic-glibc/stdio.h:878:38: error: expected function body after function declarator
    878 | extern void perror (const char *__s) __COLD;
        |                                      ^
  In file included from /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/netcdf-src-0.4.0/source/libdispatch/dcopy.c:11:
  In file included from /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/netcdf-src-0.4.0/source/include/ncdispatch.h:17:
  /opt/zig/lib/libc/include/generic-glibc/stdio.h:878:38: error: expected function body after function declarator
    878 | extern void perror (const char *__s) __COLD;
        |                                      ^
  1 error generated.
  gmake[2]: *** [libsrc/CMakeFiles/netcdf3.dir/build.make:88: libsrc/CMakeFiles/netcdf3.dir/v1hpg.c.o] Error 1
  gmake[2]: *** Waiting for unfinished jobs....
  1 error generated.
// repeated many times

I can workaround that error by just adding CFLAGS="-D__COLD=\"\"" to the compilation environment. I report this here as this either seems to be an issue in cargo-zigbuild (not setting that option) or in zig itself (__COLD) shouldn't be there.

Interestingly it works outside of the docker container with the same rust, zig, zig-build and netcdf-src version…

weiznich avatar Aug 15 '24 12:08 weiznich

https://github.com/bminor/glibc/commit/0ab341b247fc1c2cd101a87c78f38d2da00c335c

It should have been defined by glibc itself (in glibc 2.38+), can you try to inspect the code of the same file in /opt/zig/lib/libc/include/?

messense avatar Aug 16 '24 06:08 messense

Thanks for the response.

I checked it in the rust docker container and it seem like:

  • /opt/zig/lib/libc/include/generic-glibc/sys/cdefs.h does contain the relevant defines
  • /usr/include/x86_64-linux-gnu/sys/cdefs.h does not contain the relevant defines

Also ldd reports that the system uses glibc 2.36

It seems like that for some reason the system header from /usr is used and not the bundled header from zig

For reference that's with the rust:1.80.1 docker image (+ a view custom layers to install zig + cargo-zigbuild)

weiznich avatar Aug 16 '24 08:08 weiznich

Maybe it's caused by cmake? There is support for cmake in cargo-zigbuild but it's not really tested thoroughly.

https://github.com/rust-cross/cargo-zigbuild/blob/d420792226cef24e8a718dbef87fec14df77f3d2/src/zig.rs#L475-L486

messense avatar Aug 16 '24 08:08 messense

That might be the case, I'm not sure about that.

As a general note: The project I'm building depends on quite a few complex cmake projects (netcdf, hdf5, gdal, proj, just to name some). It works fine as soon as I specify that additional define manually. I believe that this means the cmake integration should be mostly fine.

weiznich avatar Aug 16 '24 12:08 weiznich

I'm also hitting this error when compiling ffmpeg-sys-next v6.1.0

--- stderr
  In file included from check.c:2:
  /home/nimi/.zigenv/versions/0.13.0/lib/libc/include/generic-glibc/stdio.h:878:38: error: expected function body after function declarator
    878 | extern void perror (const char *__s) __COLD;
1 error generated.

Adding CFLAGS="-D__COLD=\"\"" didn't seem to help:

  cargo:rerun-if-env-changed=CFLAGS
  CFLAGS = Some("-D__COLD=\"\"")

  --- stderr
  In file included from check.c:2:
  /home/nimi/.zigenv/versions/0.13.0/lib/libc/include/generic-glibc/stdio.h:878:38: error: expected function body after function declarator
    878 | extern void perror (const char *__s) __COLD;
        |                                      ^
  <command line>:3:16: note: expanded from macro '__COLD'
      3 | #define __COLD ""
        |                ^
  1 error generated.

nemosupremo avatar Apr 20 '25 18:04 nemosupremo