Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added public target_compile_features for auto and constexpr #1026

Merged
merged 2 commits into from
Apr 3, 2018

Conversation

ktonon
Copy link

@ktonon ktonon commented Mar 27, 2018

For projects which consume this header-only library using CMake, this adds public dependencies on C++ features to the library target. Requires an update to CMake 3.1.0, as target_compile_features was not available before that version.

Also, I'm not sure that the list of features is exhaustive. I only notice the use of auto and constexpr, and including these two was sufficient to get my example building without having to manually specify C++ 11.

@nlohmann
Copy link
Owner

What is the benefit compared to using

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

?

@OvermindDL1
Copy link

@nlohmann

It allows it to work on older compilers that have support for auto and so forth without all of C++11. Honestly though I'd probably just set it to C++11, it's quite literally a decade that 'most' compilers have had 'most' of its features anyway; even VS support C++11 (and C++14 and even most of C++17 too) and if it supports it then just about everything else does too.

@nlohmann
Copy link
Owner

I am not sure such a compiler exists :) We need to exclude GCC 4.8 due to bugs in the implementation, and I fear that it still would pass the feature checks.

@ktonon
Copy link
Author

ktonon commented Mar 27, 2018

CMake 3.8 adds compiler features for C++ standards: cxx_std_98, cxx_std_11, cxx_std_14, cxx_std_17 (https://cmake.org/cmake/help/v3.8/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html)

My aim with this PR is to export the dependency on C++ 11 features as an interface requirement of using this library, so that clients can just target link the library and not worry about the standard. The use of fine grained features, or just cxx_std_11, is less important.

I'm pretty new to CMake, so maybe we don't need target_compile_features to achieve this.

@ktonon
Copy link
Author

ktonon commented Mar 27, 2018

I wrapped this up as a Conan package, and consumed it like this. Without the changes in this PR, the compilation fails because C++ 11 is not manually specified.

PROJECT(TestJson)
cmake_minimum_required(VERSION 3.9)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(nlohmann_json)

add_executable(main main.cpp)

target_link_libraries(main nlohmann_json)

@ktonon
Copy link
Author

ktonon commented Mar 27, 2018

The test is dead simple:

#include <nlohmann/json.hpp>
#include <iostream>

int main() {
	nlohmann::json j;
	j["pi"] = 3.14159;
	j["happy"] = true;
	std::cout << j.dump(4) << std::endl;
	return 0;
}

and there's nothing in there that is obviously C++ 11. So my point is, if I'm consuming this library, the dependency on auto, constexpr, ... should be explicit

@OvermindDL1
Copy link

I do agree there, the minimum language feature requirements should be exported from the CMake definition.

@nlohmann
Copy link
Owner

I am not sure whether listing all C++11 features make sense here - we just use too much of them. Requiring C++11 is much saner here. The snippet from #1026 (comment) is already used for parts of the test suite. The PR should be adjusted toward something similar, and also touch the other CMakeList files.

@ktonon
Copy link
Author

ktonon commented Mar 27, 2018

@nlohmann that's fair. I think when you are ready to require a minimum version of CMake 3.8, then we can do just

target_compile_features(${NLOHMANN_JSON_TARGET_NAME} INTERFACE cxx_std_11)

Other than that, I'm not sure how to export CMAKE_CXX_STANDARD as a target requirement. Maybe set_target_properties? Or maybe not, since I don't see PUBLIC or INTERFACE modifies for that function.

Feel free to close this PR, for now I'm just patching the CMakeLists.txt file when I build with Conan.

@coveralls
Copy link

coveralls commented Mar 28, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling a35d414 on ktonon:develop into d2dd27d on nlohmann:develop.

@nlohmann
Copy link
Owner

@ktonon I would be happy with a PR implementing the changes you describe in #1026 (comment).

@ktonon
Copy link
Author

ktonon commented Mar 28, 2018

Replaced the commit. Using CMake 3.8 and cxx_std_11 now. For consistency I updated the test and benchmark CMakeLists.txt files too

@nlohmann
Copy link
Owner

Some jobs fail because of a too old CMake version. I need to adjust the .travis.yml file for that.

@nlohmann
Copy link
Owner

nlohmann commented Apr 2, 2018

@ktonon Could you please add the line brew upgrade cmake to the .travis.yml file? Here is a patch:

diff --git a/.travis.yml b/.travis.yml
index 8ce38cc8..68a16db5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -277,6 +277,7 @@ script:
      if [[ (-x $(which brew)) ]]; then
        brew update
        brew install cmake ninja
+       brew upgrade cmake
        cmake --version
      fi

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@nlohmann nlohmann self-assigned this Apr 3, 2018
@nlohmann nlohmann added this to the Release 3.1.3 milestone Apr 3, 2018
@nlohmann nlohmann merged commit 495436a into nlohmann:develop Apr 3, 2018
@nlohmann
Copy link
Owner

nlohmann commented Apr 3, 2018

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants