Skip to content

Commit 31a352a

Browse files
ChunJiaoZhaotingleby
authored andcommitted
javascript: update C++ standard to C++17 for compatibility with newer Node.js versions
When compiling mraa on Debian 13, the following error occurs: ``` /usr/include/node/node.h:696:8: error: ‘optional’ in namespace ‘std’ does not name a template type 696 | std::optional<std::string> builder_script_path; | ^~~~~~~~ /usr/include/node/node.h:696:3: note: ‘std::optional’ is only available from C++17 onwards 696 | std::optional<std::string> builder_script_path; | ^~~ ``` Root cause: Node.js version on Debian 13 is 20+, which requires C++17 feature support. Signed-off-by: Chun Jiao Zhao <[email protected]>
1 parent 28fa501 commit 31a352a

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ if (BUILDCPP)
9090
message(FATAL_ERROR "Target '${targetname}' requires c++11 which is not supported by this compiler")
9191
endif()
9292
endfunction()
93+
94+
# This function adds the c++17 flag to a c++ target (if supported)
95+
function(use_cxx_17 targetname)
96+
include(CheckCXXCompilerFlag)
97+
CHECK_CXX_COMPILER_FLAG("-std=c++17" COMPILER_SUPPORTS_CXX17)
98+
if (COMPILER_SUPPORTS_CXX17)
99+
set_target_properties(${targetname} PROPERTIES COMPILE_FLAGS "-std=c++17")
100+
else()
101+
message(FATAL_ERROR "Target '${targetname}' requires c++17 which is not supported by this compiler")
102+
endif()
103+
endfunction()
93104
endif()
94105

95106
# Set CMAKE_INSTALL_LIBDIR if not defined

src/javascript/CMakeLists.txt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,35 @@ if (BUILDCPP)
4444
# Node 0.12.x V8 engine major version is '3'.
4545
# Node 2.1.0 V8 engine major version is '4'.
4646
# Node 16.0.0 V8 engine major version is '9'.
47+
# Node 18.0.0+ requires C++17 for std::string_view, std::optional, etc.
4748
if (${V8_VERSION_MAJOR} GREATER 8)
48-
set_property (TARGET mraajs PROPERTY CXX_STANDARD 14)
49+
set_property (TARGET mraajs PROPERTY CXX_STANDARD 17)
4950
else ()
5051
set_property (TARGET mraajs PROPERTY CXX_STANDARD 11)
5152
endif ()
5253
set_property (TARGET mraajs PROPERTY CXX_STANDARD_REQUIRED ON)
5354
if (CMAKE_VERSION VERSION_LESS "3.1")
5455
message (WARNING "Need to use CMAKE version 3.1+, but it is ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}, using a workaround.")
5556
if (CMAKE_COMPILER_IS_GNUCXX)
56-
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.7")
57-
message (FATAL_ERROR "GNU gcc compiler is also too old (need 4.7+, but ${CMAKE_CXX_COMPILER_VERSION}) and does not support C++11 standard.")
57+
if (${V8_VERSION_MAJOR} GREATER 8)
58+
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7.0")
59+
message (FATAL_ERROR "GNU gcc compiler is too old (need 7.0+, but ${CMAKE_CXX_COMPILER_VERSION}) and does not support C++17 standard.")
60+
endif ()
61+
set (MRAA_CXX_WORKAROUND_OPTION "-std=gnu++17")
62+
else ()
63+
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.7")
64+
message (FATAL_ERROR "GNU gcc compiler is too old (need 4.7+, but ${CMAKE_CXX_COMPILER_VERSION}) and does not support C++11 standard.")
65+
endif ()
66+
set (MRAA_CXX_WORKAROUND_OPTION "-std=gnu++11")
5867
endif ()
59-
set (MRAA_CXX11_WORKAROUND_OPTION "-std=gnu++11")
6068
else ()
61-
set (MRAA_CXX11_WORKAROUND_OPTION "-std=c++11")
69+
if (${V8_VERSION_MAJOR} GREATER 8)
70+
set (MRAA_CXX_WORKAROUND_OPTION "-std=c++17")
71+
else ()
72+
set (MRAA_CXX_WORKAROUND_OPTION "-std=c++11")
73+
endif ()
6274
endif ()
63-
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MRAA_CXX11_WORKAROUND_OPTION} ")
75+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MRAA_CXX_WORKAROUND_OPTION} ")
6476
endif ()
6577
endif ()
6678
endif ()

0 commit comments

Comments
 (0)