Skip to content

Commit 9a5bbec

Browse files
authored
Fix string_view ABI mismatch between library and consumers (#1661)
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 d4742c2 commit 9a5bbec

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

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
@@ -125,6 +131,10 @@ if(BUILD_SHARED_LIBS)
125131

126132
target_compile_features(${SHARED_LIB} PUBLIC ${REQUIRED_FEATURES})
127133

134+
if(JSONCPP_HAS_STRING_VIEW)
135+
target_compile_definitions(${SHARED_LIB} PUBLIC JSONCPP_HAS_STRING_VIEW=1)
136+
endif()
137+
128138
target_include_directories(${SHARED_LIB} PUBLIC
129139
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
130140
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
@@ -158,6 +168,10 @@ if(BUILD_STATIC_LIBS)
158168

159169
target_compile_features(${STATIC_LIB} PUBLIC ${REQUIRED_FEATURES})
160170

171+
if(JSONCPP_HAS_STRING_VIEW)
172+
target_compile_definitions(${STATIC_LIB} PUBLIC JSONCPP_HAS_STRING_VIEW=1)
173+
endif()
174+
161175
target_include_directories(${STATIC_LIB} PUBLIC
162176
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
163177
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
@@ -184,6 +198,10 @@ if(BUILD_OBJECT_LIBS)
184198

185199
target_compile_features(${OBJECT_LIB} PUBLIC ${REQUIRED_FEATURES})
186200

201+
if(JSONCPP_HAS_STRING_VIEW)
202+
target_compile_definitions(${OBJECT_LIB} PUBLIC JSONCPP_HAS_STRING_VIEW=1)
203+
endif()
204+
187205
target_include_directories(${OBJECT_LIB} PUBLIC
188206
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
189207
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>

0 commit comments

Comments
 (0)