Skip to content

[ELF] Define NOMINMAX to fix zlib.h caused build failure on Windows #70368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2023

Conversation

yxsamliu
Copy link
Collaborator

@yxsamliu yxsamliu commented Oct 26, 2023

On Windows when zlib is enabled, zlib header introduced some Windows headers which defines max as a macro. Since OutputSections.cpp uses std::max with template argument, this causes compilation error.

Define macro NOMINMAX to avoid this.

@llvmbot
Copy link
Member

llvmbot commented Oct 26, 2023

@llvm/pr-subscribers-lld-elf

@llvm/pr-subscribers-lld

Author: Yaxun (Sam) Liu (yxsamliu)

Changes

On Windows when zlib is enabled, zlib header introduced some C headers which defines max as a macro. Since OutputSections.cpp uses std::max with template argument, this causes compilation error.

Undefine max to workaround this issue.


Full diff: https://github.com/llvm/llvm-project/pull/70368.diff

1 Files Affected:

  • (modified) lld/ELF/OutputSections.cpp (+5)
diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 2dc425927109403..df78075fee6972e 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -23,6 +23,11 @@
 #include "llvm/Support/TimeProfiler.h"
 #if LLVM_ENABLE_ZLIB
 #include <zlib.h>
+// zlib 1.2.13 causes max to be defined as a macro on Windows.
+// undefine it to use std::max.
+#ifdef max
+#undef max
+#endif
 #endif
 #if LLVM_ENABLE_ZSTD
 #include <zstd.h>

@MaskRay
Copy link
Member

MaskRay commented Oct 26, 2023

I think zlib is often disabled on Windows, so nobody has noticed for such a long time.

On Windows when zlib is enabled, zlib header introduced some C headers which defines max as a macro. Since OutputSections.cpp uses std::max with template argument, this causes compilation error.

Can you point out where max is defined in zlib v2.3.13? A likely better choice is to disable zlib for Windows if a problematic one is found.

@yxsamliu yxsamliu force-pushed the fix-OutputSections-main branch from 62d09cb to e99c4af Compare October 27, 2023 00:14
@yxsamliu
Copy link
Collaborator Author

I think zlib is often disabled on Windows, so nobody has noticed for such a long time.

On Windows when zlib is enabled, zlib header introduced some C headers which defines max as a macro. Since OutputSections.cpp uses std::max with template argument, this causes compilation error.

Can you point out where max is defined in zlib v2.3.13? A likely better choice is to disable zlib for Windows if a problematic one is found.

zlib header does not define max as a macro, but it includes some windows headers which define max as macro. There is a cleaner way to avoid that by defining a macro NOMINMAX before include zlib.h.

On Windows when zlib is enabled, zlib header introduced some
Windows headers which defines max as a macro. Since OutputSections.cpp
uses std::max with template argument, this causes compilation
error.

Define NOMINMAX before include zlib header to avoid introducing
max as a macro.
@yxsamliu yxsamliu force-pushed the fix-OutputSections-main branch from e99c4af to 27d713b Compare October 27, 2023 00:18
@yxsamliu
Copy link
Collaborator Author

yxsamliu commented Nov 1, 2023

ping

@@ -22,6 +22,8 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/TimeProfiler.h"
#if LLVM_ENABLE_ZLIB
// Avoid introducing max as a macro from Windows headers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Avoid introducing max as a macro from windows.h on Windows.

@MaskRay MaskRay changed the title Fix build failure of lld/ELF/OutputSections.cpp [ELF] Define NOMINMAX to fix zlib.h caused build failure on Windows Nov 1, 2023
@MaskRay
Copy link
Member

MaskRay commented Nov 1, 2023

Thanks. The original title was not very clear. I have changed it to
[ELF] Define NOMINMAX to fix zlib.h caused build failure on Windows

@yxsamliu yxsamliu merged commit 3594769 into llvm:main Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants