osm2pgsql icon indicating copy to clipboard operation
osm2pgsql copied to clipboard

Declaring C++ version in CMake config

Open joto opened this issue 5 years ago • 5 comments

osm2pgsql currently compiles with C++11 or newer standards. For some libraries it might be better to compile in C++14 mode (or even newer) if it is available. This is the case for fmt which we recently introduced in osm2pgsql, which can use compile-time checking of format strings in C++14 but not C++11. I think the policy should be:

  • if the user sets a version, use that one
  • if your compiler has a default version >= C++11, use that one
  • if your compiler has a default version < C++11, use C++11

Is that the right policy?

Currently the CMake config hard-codes the choice (with C++11 being the default and manual change possible). How can we make the policy above work in our CMake config?

joto avatar Dec 03 '19 13:12 joto

Just as a side note: GCC 5 and Clang 3.9 use C++98 as default, but both do support C++11 when asked, so there probably are a good number of compilers out there which do need the -std=c++11 option to compile osm2pgsql, we can't just get rid of it.

joto avatar Dec 03 '19 13:12 joto

Policy sounds okay but whatever we allow to set we should have a Travis test for. Currently we test against C++11 and C++14.

lonvia avatar Jan 28 '20 20:01 lonvia

It looks like what we want is target_compile_features(osm2pgsql PUBLIC cxx_std_11), but the cxx_std_11 feature is only available from CMake 3.8 and we still support older versions of CMake. It's not worth it to complicate our CMake config for this doing extra checks or so.

So lets shelve this and revisit in a year or so.

joto avatar Aug 06 '20 12:08 joto

We have now switched to C++14. Since GCC 6.1 and Clang 6 the compiler default is C++14 anyway. I think we can safely remove any kind of version setting in the CMake config and just go with the compiler defaults.

joto avatar May 27 '21 09:05 joto

I have experimented with that and it seems setting

# We don't want to use special compiler extensions because we want portability
set(CMAKE_CXX_EXTENSIONS OFF)

target_compile_features(osm2pgsql_lib PUBLIC cxx_std_17)

should work, because it has (mostly) the desired behaviour of setting --std=c++17 if needed and nothing otherwise. It doesn't work for some reason on macOS though, I suspect that clang on macOS defaults to C++98 and CMake on macOS doesn't know this, so it doesn't set anything. Bummer.

There is also the problem of the fmt lib, see #1704 .

joto avatar Jul 26 '22 07:07 joto