Skip to content

Commit 004ecbd

Browse files
committed
Fix string_view ABI mismatch between library and consumers
The library was forced to build with CMAKE_CXX_STANDARD 11, so JSONCPP_HAS_STRING_VIEW was never defined at compile time. Consumers building with C++17 would see the string_view APIs in the header but fail to link them. Fix: - Remove the global CMAKE_CXX_STANDARD 11 override; the existing target_compile_features(cxx_std_11) already enforces the minimum. - Detect string_view support at configure time with check_cxx_source_compiles and export JSONCPP_HAS_STRING_VIEW as a PUBLIC compile definition on all library targets, so consumers always see the same value the library was built with. - Guard the __cplusplus fallback in value.h so it does not override the CMake-set define. Fixes #1595
1 parent ef08771 commit 004ecbd

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ foreach(pold "") # Currently Empty
4040
endif()
4141
endforeach()
4242

43-
# Build the library with C++11 standard support, independent from other including
44-
# software which may use a different CXX_STANDARD or CMAKE_CXX_STANDARD.
45-
set(CMAKE_CXX_STANDARD 11)
46-
set(CMAKE_CXX_EXTENSIONS OFF)
47-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
48-
4943
# Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators.
5044
if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
5145
set(CMAKE_BUILD_TYPE Release CACHE STRING

include/json/value.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
#endif
4040
#endif
4141

42+
#ifndef JSONCPP_HAS_STRING_VIEW
4243
#if __cplusplus >= 201703L
4344
#define JSONCPP_HAS_STRING_VIEW 1
4445
#endif
46+
#endif
4547

4648
#include <array>
4749
#include <exception>

src/lib_json/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ include(CheckIncludeFileCXX)
77
include(CheckTypeSize)
88
include(CheckStructHasMember)
99
include(CheckCXXSymbolExists)
10+
include(CheckCXXSourceCompiles)
1011

1112
check_include_file_cxx(clocale HAVE_CLOCALE)
1213
check_cxx_symbol_exists(localeconv clocale HAVE_LOCALECONV)
@@ -25,6 +26,11 @@ if(NOT (HAVE_CLOCALE AND HAVE_LCONV_SIZE AND HAVE_DECIMAL_POINT AND HAVE_LOCALEC
2526
endif()
2627
endif()
2728

29+
check_cxx_source_compiles(
30+
"#include <string_view>
31+
int main() { std::string_view sv; return 0; }"
32+
JSONCPP_HAS_STRING_VIEW)
33+
2834
set(JSONCPP_INCLUDE_DIR ../../include)
2935

3036
set(PUBLIC_HEADERS
@@ -129,6 +135,10 @@ if(BUILD_SHARED_LIBS)
129135

130136
target_compile_features(${SHARED_LIB} PUBLIC ${REQUIRED_FEATURES})
131137

138+
if(JSONCPP_HAS_STRING_VIEW)
139+
target_compile_definitions(${SHARED_LIB} PUBLIC JSONCPP_HAS_STRING_VIEW=1)
140+
endif()
141+
132142
target_include_directories(${SHARED_LIB} PUBLIC
133143
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
134144
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
@@ -162,6 +172,10 @@ if(BUILD_STATIC_LIBS)
162172

163173
target_compile_features(${STATIC_LIB} PUBLIC ${REQUIRED_FEATURES})
164174

175+
if(JSONCPP_HAS_STRING_VIEW)
176+
target_compile_definitions(${STATIC_LIB} PUBLIC JSONCPP_HAS_STRING_VIEW=1)
177+
endif()
178+
165179
target_include_directories(${STATIC_LIB} PUBLIC
166180
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
167181
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
@@ -188,6 +202,10 @@ if(BUILD_OBJECT_LIBS)
188202

189203
target_compile_features(${OBJECT_LIB} PUBLIC ${REQUIRED_FEATURES})
190204

205+
if(JSONCPP_HAS_STRING_VIEW)
206+
target_compile_definitions(${OBJECT_LIB} PUBLIC JSONCPP_HAS_STRING_VIEW=1)
207+
endif()
208+
191209
target_include_directories(${OBJECT_LIB} PUBLIC
192210
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
193211
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>

0 commit comments

Comments
 (0)