From c505fd9c1a3809e564abbf34be842b3ffab6d680 Mon Sep 17 00:00:00 2001 From: Amar Mahmutbegovic Date: Wed, 3 Jul 2024 18:54:04 +0200 Subject: [PATCH] update error_handling project CMake file --- .../error_handling/.vscode/settings.json | 44 ++++++++++++++++++- Chapter07/error_handling/CMakeLists.txt | 30 +++++++++---- .../app/src/main_exceptions.cpp | 22 +++++++++- 3 files changed, 84 insertions(+), 12 deletions(-) diff --git a/Chapter07/error_handling/.vscode/settings.json b/Chapter07/error_handling/.vscode/settings.json index d9b0cb6..b453fcd 100644 --- a/Chapter07/error_handling/.vscode/settings.json +++ b/Chapter07/error_handling/.vscode/settings.json @@ -2,7 +2,49 @@ "files.associations": { "span": "cpp", "array": "cpp", - "string_view": "cpp" + "string_view": "cpp", + "exception": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" }, "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/Chapter07/error_handling/CMakeLists.txt b/Chapter07/error_handling/CMakeLists.txt index de03921..95e2535 100644 --- a/Chapter07/error_handling/CMakeLists.txt +++ b/Chapter07/error_handling/CMakeLists.txt @@ -9,7 +9,23 @@ set(CMAKE_OBJCOPY "arm-none-eabi-objcopy") set(CMAKE_SIZE "arm-none-eabi-size") set(RENODE "renode" CACHE STRING "Path to Renode executable") -message(STATUS "Using Renode at: ${RENODE}") + +set(MAIN_CPP_PATH "${CMAKE_SOURCE_DIR}/app/src/") +set(MAIN_CPP_FILE_NAME "main_assert.cpp" CACHE STRING "main file") +list(APPEND LIB_SPECS "-specs=nosys.specs") + +set(EXCEPTIONS_FLAGS "-fno-exceptions -fno-rtti") + +if("${MAIN_CPP_FILE_NAME}" STREQUAL "main_exceptions.cpp") + message(STATUS "Enabling exceptions") + set(EXCEPTIONS_FLAGS "-fexceptions") + set(LIB_SPECS "-specs=nosys.specs") +else() + message(STATUS "Exceptions are disabled") + list(APPEND LIB_SPECS "-specs=nano.specs") +endif() +message(STATUS "Exceptions flags set to: ${EXCEPTIONS_FLAGS}") +message(STATUS "Lib specs set to: ${LIB_SPECS}") find_program(CCACHE_FOUND ccache) @@ -23,11 +39,11 @@ set(CDEFS "-DUSE_HAL_DRIVER -DSTM32F072xB") set(MCU "-mcpu=cortex-m0 -mthumb") set(COMMON_FLAGS "${MCU} ${CDEFS} -fdata-sections -ffunction-sections -Wno-address-of-packed-member -Wall -Wextra -Wno-unused-parameter") set(CMAKE_C_FLAGS "${COMMON_FLAGS}") -set(CMAKE_CXX_FLAGS "${COMMON_FLAGS} -Wno-register -fno-exceptions -fno-rtti -fno-threadsafe-statics") +set(CMAKE_CXX_FLAGS "${COMMON_FLAGS} -Wno-register ${EXCEPTIONS_FLAGS} -fno-threadsafe-statics") set(CMAKE_ASM_FLAGS "${COMMON_FLAGS} -x assembler-with-cpp") -set(CMAKE_C_FLAGS_DEBUG "-g -gdwarf-2 -O1") -set(CMAKE_CXX_FLAGS_DEBUG "-g -gdwarf-2 -O1") +set(CMAKE_C_FLAGS_DEBUG "-g -gdwarf-2 -O0") +set(CMAKE_CXX_FLAGS_DEBUG "-g -gdwarf-2 -O0") set(CMAKE_C_FLAGS_RELEASE "-O2 -flto") set(CMAKE_CXX_FLAGS_RELEASE "-O2 -flto") set(CMAKE_C_FLAGS_MINSIZEREL "-Os -flto") @@ -83,9 +99,6 @@ include_directories( ${CMAKE_SOURCE_DIR}/cstdlib_support ) -set(MAIN_CPP_PATH "${CMAKE_SOURCE_DIR}/app/src/") -set(MAIN_CPP_FILE_NAME "main_assert.cpp" CACHE STRING "main") - set(EXECUTABLE ${PROJECT_NAME}.elf) add_executable( @@ -118,8 +131,7 @@ target_link_options( -T${CMAKE_SOURCE_DIR}/platform/STM32F072C8Tx_FLASH.ld -mcpu=cortex-m0 -mthumb - -specs=nano.specs - -specs=nosys.specs + ${LIB_SPECS} -lnosys -u -lc diff --git a/Chapter07/error_handling/app/src/main_exceptions.cpp b/Chapter07/error_handling/app/src/main_exceptions.cpp index 34e5d1f..4bd5cd1 100644 --- a/Chapter07/error_handling/app/src/main_exceptions.cpp +++ b/Chapter07/error_handling/app/src/main_exceptions.cpp @@ -1,7 +1,9 @@ #include #include #include +#include #include +#include #include @@ -39,11 +41,27 @@ int main() std::array arr; - arr.at(5) = 6; + //arr.at(5) = 6; - printf("Exceptions example\r\n"); + //throw 1; + try { + //throw 1; + arr.at(5) = 6; + + } + catch (int i) { + printf("i = %d\r\n", i); + } + catch(std::out_of_range) { + printf("out of range\r\n"); + } + catch (...) { + printf("Catch\r\n"); + } + while(true) { } } + \ No newline at end of file