mmp / pbrt-v4

Source code to pbrt, the ray tracer described in the forthcoming 4th edition of the "Physically Based Rendering: From Theory to Implementation" book.
https://pbrt.org
Apache License 2.0
2.83k stars 440 forks source link

Compilation error in Windows on VS2019, std::max is not a member of std #165

Closed trevordblack closed 3 years ago

trevordblack commented 3 years ago

Ran into a problem compiling pbrt.exe on:

Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.19043.
...
Found CUDA: 11.4.100
...
Detected CUDA Architecture: sm_75

Optix 7.3 and Visual Studio 2019.

Where the problem is:

  pbrt.cpp
C:\Users\Trevor\pbrt-v4\src\pbrt/util/float.h(301,17): error C2039: 'max': is not a member of 'std'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\iterator(21): message : see declaration of 'std'
C:\Users\Trevor\pbrt-v4\src\pbrt/util/float.h(301,20): error C2065: 'max': undeclared identifier
C:\Users\Trevor\pbrt-v4\src\pbrt/util/float.h(301,21): error C2275: 'pbrt::Float': illegal use of this type as an expression
C:\Users\Trevor\pbrt-v4\src\pbrt/pbrt.h(96): message : see declaration of 'pbrt::Float'

git blame says that that specific part of the codebase hasn't been changed since October of last year.

Given the combinatorial explosion of OS, cuda, OptiX, and compiler, it's possible that this will be a problem unique to me, but I'm documenting it here for anyone in the future.

trevordblack commented 3 years ago

Following along with the discussion on this board: https://social.msdn.microsoft.com/Forums/vstudio/en-US/17814e4a-e997-4f28-9878-6cd8303e390d/visual-studio-is-fine-with-stdmax-unless-its-in-one-particular-static-library

I made a small change to float.h:

// pbrt is Copyright(c) 1998-2020 Matt Pharr, Wenzel Jakob, and Greg Humphreys.
// The pbrt source code is licensed under the Apache License, Version 2.0.
// SPDX: Apache-2.0

#ifndef PBRT_UTIL_FLOAT_H
#define PBRT_UTIL_FLOAT_H

#include <pbrt/pbrt.h>

#include <pbrt/util/pstd.h>

#include <algorithm> // this is new
#include <cmath>
#include <cstdint>
#include <cstring>
#include <limits>
#include <string>

The code now compiles without error.

There are additional suggestions at https://stackoverflow.com/questions/2789481/problem-calling-stdmax that may provide a more robust, long-term solution (if this ends up being a consistent problem)

mmp commented 3 years ago

Thanks for reporting this! I've fixed that as well as some other headers that were sloppy about using things in <algorithm> without including it, happening to get the definitions because one of their other #includes had it included...