yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
900 stars 112 forks source link

C++ 17 detection seems wonky on visual studio #186

Closed rbtcollins closed 2 years ago

rbtcollins commented 2 years ago

Even with the cl flag -std:c++17 - which successfully builds the demo from README.md

I've commented it out in the code I'm using, but it would be nice if this was addressed upstream somehow.

yhirose commented 2 years ago

@rbtcollins, thanks for the feedback. I don't understand what you are talking about. Could you explain it more clearly? Thanks!

rbtcollins commented 2 years ago

I can try ;).

My development environment for this project is VS Code + The Visual Studio 2022 Community edition C++ Compiler toolchain, building using CMake + Ninja.

I'm building in C++17 mode- in CMakeLists.txt:

target_compile_features(lib_common PRIVATE cxx_std_17)

This causes the compiler (cl)to receive the flag -std:c++17 on the command line, which enables C++ 17 semantics. When set the __cplusplus macro value is 199711.

Because of this I had to modify peglib.h to make my parser compile. With the modification the parser seems to work fine - I have ~ 1MB of data, and it all gets processed and I get back the top level value as expected.

The change I made is trivial - just commenting out the __cplusplus version check at the top of peglib.h.

yhirose commented 2 years ago

@rbtcollins, thanks. I now fully understand your situation. :)

It's a simply problem with how to handle __cplusplus in MSVC, and also a longstanding known issue. You can see more information here: https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/

The following article is helpful as well: https://discourse.cmake.org/t/set-cmake-cxx-standard-should-set-zc-cplusplus-for-msvc/1876

As the documents mention, you need to enable the /Zc:__cplusplus option. Please see my CMakeLists.txt:

https://github.com/yhirose/cpp-peglib/blob/4d67d96d7ace0830549908a6fae9064d74683f5a/CMakeLists.txt#L4-L11

Hope this helps!