ocaml / dune

A composable build system for OCaml.
https://dune.build/
MIT License
1.61k stars 399 forks source link

Missing -std=c++11 when compiling C++ files #10886

Open kit-ty-kate opened 3 weeks ago

kit-ty-kate commented 3 weeks ago

See https://github.com/ocaml/ocaml/issues/13422 OCaml 5.3 requires -std=c++11 to be passed when C++ is used. From what i understand Dune 3 passes +x c++ automatically to the C compiler so it should probably also add this option too.

MisterDA commented 3 weeks ago

To be pendantic I suggest that Dune checks that the C++ compiler it uses defaults at least to C++11. This can be checked with cc-style compilers by:

$ cc -dM -E -x c++ - < /dev/null | grep __cplusplus
#define __cplusplus 199711L
$ cc --version
Apple clang version 15.0.0 (clang-1500.3.9.4)
$ /opt/homebrew/Cellar/llvm/18.1.8/bin/clang -dM -E -x c++ - < /dev/null | grep __cplusplus
#define __cplusplus 201703L
$ /opt/homebrew/bin/gcc-14 -dM -E -x c++ - < /dev/null | grep __cplusplus
#define __cplusplus 201703L

You see here that Apple clang 15 defaults to C++98, but clang and gcc installed via Homebrew default to C++17.

MSVC defaults to C++14, so that's fine.

MisterDA commented 1 week ago

Note that clang-16 shipped with the new macOS 15.0 Sequoia still defaults to C++98.

rgrinberg commented 1 week ago

@MisterDA do you want to send a PR?

MisterDA commented 1 week ago

I don't think that passing a specific version of C++ is the way to go… I would rather have Dune detect the version of C++ the C++ compiler defaults to, if there are C++ stubs, and pass -std=c++11 if it default to a version older than C++11. I'm not sure where that could go in Dune.

kit-ty-kate commented 1 week ago

I don't think that passing a specific version of C++ is the way to go… I would rather have Dune detect the version of C++ the C++ compiler defaults to, if there are C++ stubs, and pass -std=c++11 if it default to a version older than C++11.

If this is to avoid issues with having the option overwrite the default or a user-given -std=[...] i don't think these are a problem at all:

MisterDA commented 1 week ago

Makes sense.

the default would be overwritten only for OCaml 5.3 so it should be detected early when users upgrade

We could also default to C++11 for all OCaml 5 versions.

kit-ty-kate commented 1 day ago

I opened https://github.com/ocaml/dune/pull/10962 to fix this