randomenv: Fix MinGW dllimport warning for environ #33570

pull l0rinc wants to merge 1 commits into bitcoin:master from l0rinc:l0rinc/environ-mingw changing 1 files +1 −1
  1. l0rinc commented at 1:14 am on October 8, 2025: contributor

    Related to #33550 (comment)

    Extends 7703884 to guard environ declaration on all Windows builds, not just MSVC.

    In the mingw-w64 headers (used by llvm-mingw), environ is defined as a macro which expands through _environ to (* __p__environ()), a call to a dllimport function, causing the same inconsistent linkage warning as MSVC.

    Use WIN32 instead of _MSC_VER to match the platform-specific guards already used throughout the file.

    The warning occurs with llvm-mingw (both UCRT and MSVCRT variants as tested by Hebasto), but not with the mingw-w64 toolchain currently used in CI (as mentioned by fanquake).


    The error was reproduced by adding a temporary nightly build pointing to https://github.com/l0rinc/bitcoin/pull/45. On master the failure can be seen in https://github.com/l0rinc/bitcoin-core-nightly/pull/2

    before: https://github.com/l0rinc/bitcoin-core-nightly/actions/runs/18327936488/job/52196728885?pr=2

     0/home/runner/work/bitcoin-core-nightly/bitcoin-core-nightly/src/randomenv.cpp:61:15: warning: '__p__environ' redeclared without 'dllimport' attribute: previous 'dllimport' ignored [-Winconsistent-dllimport]
     1   61 | extern char** environ; // NOLINT(readability-redundant-declaration): Necessary on some platforms
     2      |               ^
     3/home/runner/work/bitcoin-core-nightly/bitcoin-core-nightly/llvm_mingw_toolchain/aarch64-w64-mingw32/include/stdlib.h:656:17: note: expanded from macro 'environ'
     4  656 | #define environ _environ
     5      |                 ^
     6/home/runner/work/bitcoin-core-nightly/bitcoin-core-nightly/llvm_mingw_toolchain/aarch64-w64-mingw32/include/stdlib.h:225:21: note: expanded from macro '_environ'
     7  225 | #define _environ (* __p__environ())
     8      |                     ^
     9/home/runner/work/bitcoin-core-nightly/bitcoin-core-nightly/llvm_mingw_toolchain/aarch64-w64-mingw32/include/stdlib.h:221:27: note: previous declaration is here
    10  221 |   _CRTIMP char ***__cdecl __p__environ(void);
    11      |                           ^
    12/home/runner/work/bitcoin-core-nightly/bitcoin-core-nightly/llvm_mingw_toolchain/aarch64-w64-mingw32/include/stdlib.h:221:3: note: previous attribute is here
    13  221 |   _CRTIMP char ***__cdecl __p__environ(void);
    14      |   ^
    15/home/runner/work/bitcoin-core-nightly/bitcoin-core-nightly/llvm_mingw_toolchain/aarch64-w64-mingw32/include/_mingw.h:52:40: note: expanded from macro '_CRTIMP'
    16   52 | #      define _CRTIMP  __attribute__ ((__dllimport__))
    17      |                                        ^
    181 warning generated.
    

    after: https://github.com/l0rinc/bitcoin-core-nightly/actions/runs/18329616268/job/52201940831?pr=4

    0[ 28%] Building CXX object src/util/CMakeFiles/bitcoin_util.dir/__/randomenv.cpp.obj
    

    Note that there are some other remaining warnings in the logs that will be fixed in separate PRs

  2. DrahtBot commented at 1:14 am on October 8, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33570.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK hebasto, sipa, laanwj

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

  3. maflcko added the label DrahtBot Guix build requested on Oct 8, 2025
  4. fanquake commented at 9:05 am on October 8, 2025: member

    MinGW also defines environ … causing the same inconsistent linkage warning.

    Can you improve the PR description & commit message? We use mingw-w64 in the CI & Guix build, and it doesn’t produce this warning.

  5. hebasto commented at 11:30 am on October 8, 2025: member

    Extends 7703884 to guard environ declaration on all Windows builds, not just MSVC. MinGW also defines environ as a macro that expands to a dllimport function, causing the same inconsistent linkage warning.

    I suppose this behaviour is specific to UCRT.

    UPDATE. It is not. I tested the MSVCRT variant of the LLVM MinGW toolchain.

  6. l0rinc commented at 5:41 pm on October 8, 2025: contributor
    Thanks for the replies, I have updated the description to clarify this is llvm-mingw specific, let me know if that’s enough.
  7. DrahtBot commented at 1:29 am on October 9, 2025: contributor

    Guix builds (on x86_64) [untrusted test-only build, possibly unsafe, not for production use]

    File commit b510893d00760083ac36948747aa6ebd84656192(master) commit 883c8fecd5e61b32cbcba08dff4f9f876dae3be0(pull/33570/merge)
    *-aarch64-linux-gnu-debug.tar.gz 287ee038591e1aa5... 5451ddd49d8bbb4f...
    *-aarch64-linux-gnu.tar.gz 0852b8c58d9f260d... 25bd538478f9eb41...
    *-arm-linux-gnueabihf-debug.tar.gz 476283a97a4a63bb... a7f5052cdbf7f7a1...
    *-arm-linux-gnueabihf.tar.gz 9521ef406263f7b6... 5a4451c798e27c33...
    *-arm64-apple-darwin-codesigning.tar.gz 82cb6250417e717c... 7de7cc4af586b871...
    *-arm64-apple-darwin-unsigned.tar.gz 8fc23c4c3a991331... e3d67510950c2c81...
    *-arm64-apple-darwin-unsigned.zip b392101c5b361ff9... 4be0697eeada46e2...
    *-powerpc64-linux-gnu-debug.tar.gz 9b43c0b8e25143cf... ce7c3c8574b012be...
    *-powerpc64-linux-gnu.tar.gz 2e9992ef3509b037... 6b5a20204f8083df...
    *-riscv64-linux-gnu-debug.tar.gz ecf900527a72acb0... a6ae0c793a3c2adb...
    *-riscv64-linux-gnu.tar.gz 9d408e2da915b801... fc0624b26a20a94a...
    *-x86_64-apple-darwin-codesigning.tar.gz 9bf52d02ca33fe53... 93c133a51bd98292...
    *-x86_64-apple-darwin-unsigned.tar.gz 236075db81a09bad... 6337f4e8fe8d1973...
    *-x86_64-apple-darwin-unsigned.zip 0c441dc5b75521ac... 129c615e359fe503...
    *-x86_64-linux-gnu-debug.tar.gz 6f97bc61fc54c68f... 38494443fe352c07...
    *-x86_64-linux-gnu.tar.gz 16459e49234e829e... a674ad52e9ab239b...
    *.tar.gz dd7765ebe3b225dd... 7681d1dcb1b82dc3...
    SHA256SUMS.part 63ea3df1b92207e2... 34bacbe246873834...
    guix_build.log 266ffcacd115224c... ea50795b0508ce5e...
    guix_build.log.diff 225bb976da2b3471...
  8. DrahtBot removed the label DrahtBot Guix build requested on Oct 9, 2025
  9. hebasto approved
  10. hebasto commented at 10:32 am on October 9, 2025: member
    ACK 97762b1fcf5d389b4f9c06ae46d6408fb78627b0.
  11. maflcko commented at 10:59 am on October 9, 2025: member
    Reminds me that the windows guix build is actually disabled in DrahtBot: https://github.com/maflcko/DrahtBot/blob/cd13339b9c72a12dc47931cd40e1c8872d9ac74d/guix/src/main.rs#L299, so pls ignore the result above, sry.
  12. fanquake commented at 10:58 am on October 14, 2025: member

    Thanks for the replies, I have updated the description to clarify this is llvm-mingw specific, let me know if that’s enough.

    You should update the commit message to match.

    llvm-mingw also defines environ as a macro

    Can you be more clear with this explanation. llvm-mingw looks like a collection of scripts that download and build LLVM and the mingw-w64 headers. I don’t think it defines anything environ related?

  13. randomenv: Fix MinGW dllimport warning for `environ`
    Extends 7703884 to guard environ declaration on all Windows builds, not just MSVC.
    In the mingw-w64 headers (used by llvm-mingw), environ is defined as a macro which expands through _environ to (* __p__environ()), a call to a dllimport function, causing the same inconsistent linkage warning as MSVC.
    
    Use WIN32 instead of _MSC_VER to match the platform-specific guards already used throughout the file.
    
    The warning occurs with llvm-mingw (both UCRT and MSVCRT variants as tested by Hebasto), but not with the mingw-w64 toolchain currently used in CI (as mentioned by fanquake).
    
    Co-authored-by: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
    9610b0d1e2
  14. l0rinc force-pushed on Oct 14, 2025
  15. l0rinc commented at 2:26 pm on October 14, 2025: contributor
  16. hebasto approved
  17. hebasto commented at 4:58 pm on October 16, 2025: member
    re-ACK 9610b0d1e28aeda02a2ddcf1f0591ae577c3e88e.
  18. hebasto commented at 5:03 pm on October 16, 2025: member

    We use mingw-w64 in the CI & Guix build, and it doesn’t produce this warning.

    GCC has no such a diagnostic: https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html.

    Clang does: https://clang.llvm.org/docs/DiagnosticsReference.html#winconsistent-dllimport.

  19. hebasto commented at 8:41 am on October 22, 2025: member
    Friendly ping people who reviewed the previous change: @sipa @sipsorcery @theuni
  20. sipa commented at 8:44 am on October 22, 2025: member
    utACK 9610b0d1e28aeda02a2ddcf1f0591ae577c3e88e if this makes the compilers happy
  21. laanwj commented at 9:20 am on October 22, 2025: member

    After discussion i’m convinced that this is the right solution. According to man pages, on POSIX (non-windows) platforms, including MacOS, you are supposed to define this variable yourself. So it can’t be removed entirely.

    On Windows (irrespective of compiler or toolchain) it’s provided by the headers, and defined in a different way, so if we want to use environ therel, we shouldn’t define it ourselves.

    Note that it’s deprecated though:

    The _environ variable is a pointer to an array of pointers to the multibyte-character strings that constitute the process environment. This global variable has been deprecated for the more secure functional versions getenv_s, _wgetenv_s and _putenv_s, _wputenv_s, which should be used in place of the global variable. _environ is declared in Stdlib.h.

    Anyhow, this is the best way to fix it while keeping the functionality as-is.

    Code review ACK 9610b0d1e28aeda02a2ddcf1f0591ae577c3e88e

  22. hebasto approved
  23. hebasto commented at 9:50 am on October 22, 2025: member

    My Guix build:

    0917a1df9f8f548b0c6d99bce8bb4774e7095480f7a5a14d4a0d724db78204e98  guix-build-9610b0d1e28a/output/dist-archive/bitcoin-9610b0d1e28a.tar.gz
    13372aedd6ed25924fc6e22596114ad8d9a994fb4c1342e1308b4fdbb7a38bdf7  guix-build-9610b0d1e28a/output/x86_64-w64-mingw32/SHA256SUMS.part
    28e2bd977f7b6ef6993361d3b49fec11e5d92e6ed72142d9b25896cc4412e2655  guix-build-9610b0d1e28a/output/x86_64-w64-mingw32/bitcoin-9610b0d1e28a-win64-codesigning.tar.gz
    3fd160165f6e496f0f498b56a467dea67af2c4f341326ea56e5232133af6df7c5  guix-build-9610b0d1e28a/output/x86_64-w64-mingw32/bitcoin-9610b0d1e28a-win64-debug.zip
    4220f4dbaf25707986ca0320fa33711383d89ea1a56889f41dc5efa634c348072  guix-build-9610b0d1e28a/output/x86_64-w64-mingw32/bitcoin-9610b0d1e28a-win64-setup-unsigned.exe
    56696f40cdb46fb4e6f67988cd6fd810133b3b1efe99c5f92980f8b22461d6c7a  guix-build-9610b0d1e28a/output/x86_64-w64-mingw32/bitcoin-9610b0d1e28a-win64-unsigned.zip
    

    bitcoind.exe from bitcoin-9610b0d1e28a-win64-unsigned.zip has been tested on Windows 11.

  24. hebasto merged this on Oct 22, 2025
  25. hebasto closed this on Oct 22, 2025

  26. l0rinc deleted the branch on Oct 22, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-10-31 15:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me