NVIDIA / NVIDIA/cccl

[FEA]: Add `/Zc:__cplusplus` to `CMAKE_CXX_FLAGS` and `CMAKE_CUDA_FLAGS`

Open
#2,072 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
2.5k
Forks
486
Avg merge
2d 6h
Merged PRs (30d)
295

Description

### Is this a duplicate?

- [X] I confirmed there appear to be no [duplicate issues](https://github.com/NVIDIA/cccl/issues) for this request and that I agree to the [Code of Conduct](CODE_OF_CONDUCT.md)

### Area

General CCCL

### Is your feature request related to a problem? Please describe.

On Windows, when using Visual Studio, `__cplusplus` defaults to `199711L` to communicate support for C++98. This use to be hard-coded in the compiler. Fortunately [a few years back]( https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/ ) they changed this behavior. However it requires a flag be passed during compilation, `/Zc:__cplusplus`. This also needs to be passed by NVCC when calling the host compiler

Mention this as the logic in CCCL appears to check the C++ version ( added in commit: https://github.com/NVIDIA/cccl/commit/01aa61c6a75c83a02460276b0cf9753f89688205 ). What is already here seems ok on Windows. [These macros]( https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170 ) are defined

https://github.com/NVIDIA/cccl/blob/82a3ed0282893d6316abde04394f59a9a37a3747/libcudacxx/include/nv/target#L27-L30

Though adding `/Zc:__cplusplus` might help simplify the code (and avoid needing to reuse this pattern in multiple places)
```

### Describe the solution you'd like

It looks like CUTLASS implemented this by adding [the following logic in their `CMakeLists.txt`]( https://github.com/NVIDIA/cutlass/blob/56b46e2d13875b46b8f6a03f9f5ac91e2bfdc01a/CMakeLists.txt#L440-L454 ):

```CMake
if (MSVC)

# MSVC by default does not apply the correct __cplusplus version as specified by the C++ standard
# because MSVC is not a completely compliant implementation. This option forces MSVC to use the
# appropriate value given the requested --std option. This fixes a compilation issue mismatch
# between GCC/Clang and MSVC.
#
# error : a constexpr function cannot have a nonliteral return type "dim3"
#
# See https://developercommunity.visualstudio.com/t/msvc-incorrectly-defines-cplusplus/139261

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler /Zc:__cplusplus")

endif()
```

Perhaps a similar approach can be taken here. Though as CCCL is used in the compilation of other things, maybe adding these flags with [`target_compile_options`]( https://cmake.org/cmake/help/latest/command/target_compile_options.html#command:target_compile_options ) or similar would be the way to go

### Describe alternatives you've considered

The existing code here functions. So it is fine to keep

Maybe this problem could be solved in CMake more generally? There is [an open upstream issue]( https://gitlab.kitware.com/cmake/cmake/-/issues/18837 ). This would benefit a variety of projects

Perhaps with discussion Microsoft could make add this flag default behavior. Then no code changes are needed anywhere

Admittedly all of these things can take time. So having other solutions in interim can be helpful

### Additional context

Was debugging XGBoost Windows builds and learned about this flag

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.