Reputation: 11
I'm facing an issue with programming C++ classes for an STM32F4 microcontroller. To simplify my problem, I want to create classes that can be called from my main code. However, I'm encountering a problem with including the necessary libraries.
The main issue arises when I use functions from the HAL (Hardware Abstraction Layer) library in both my main code and the classes I've created (let's call the custom class "CANBert"). The class "CANBert" fails to compile if the HAL library is not included. However, if I include the "CANBert" class in the main code, the debugger complains about duplicate inclusion of the standard libraries.
To address this problem, I had the idea of placing my new classes between the main code and the inclusion of the HAL libraries in the inclusion hierarchy. However, the debugger still raises an issue. I'm unsure how to resolve these inclusion problems.
For testing the inclusion of classes in the predefined code from the CubeIDE i tried to blink an LED over the extern class blinky.
Here is my first try:
/*
* Blinky.h
*
* Created on: Jul 1, 2023
*
*/
#ifndef BLINKY_H_
#define BLINKY_H_
#include "stm32f4xx_hal.h"
class Blinky final {
public:
Blinky();
virtual ~Blinky();
void blink(int del);
};
#endif /* BLINKY_H_ */
Here is the c file:
/*
* Blinky.cpp
*
* Created on: Jul 1, 2023
*
*/
#include "Blinky.h"
Blinky::Blinky() {
// TODO Auto-generated constructor stub
}
Blinky::~Blinky() {
// TODO Auto-generated destructor stub
}
void Blinky::blink(int del) {
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_Delay(delay);
}
and the inclusion in the main:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Blinky.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : LED_Pin */
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
I'll get the forlowing code from the debugger:
10:09:31 **** Incremental Build of configuration Debug for project Class_Test ****
make -j8 all
arm-none-eabi-gcc "../Core/Src/main.c" -mcpu=cortex-m4 -std=gnu11 -g3 -DDEBUG -DUSE_HAL_DRIVER -DSTM32F446xx -c -I../Core/Inc -I../Drivers/STM32F4xx_HAL_Driver/Inc -I../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32F4xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -fcyclomatic-complexity -MMD -MP -MF"Core/Src/main.d" -MT"Core/Src/main.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Src/main.o"
arm-none-eabi-g++ "../Core/Blinky.cpp" -mcpu=cortex-m4 -std=gnu++14 -g3 -DDEBUG -DUSE_HAL_DRIVER -DSTM32F446xx -c -I../Core/Inc -I../Drivers/STM32F4xx_HAL_Driver/Inc -I../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32F4xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -fno-exceptions -fno-rtti -fno-use-cxa-atexit -Wall -fstack-usage -fcyclomatic-complexity -MMD -MP -MF"Core/Blinky.d" -MT"Core/Blinky.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Blinky.o"
../Core/Src/main.c:25:10: fatal error: Blinky.h: No such file or directory
25 | #include "Blinky.h"
| ^~~~~~~~~~
compilation terminated.
make: *** [Core/Src/subdir.mk:34: Core/Src/main.o] Error 1
make: *** Waiting for unfinished jobs....
../Core/Blinky.cpp: In member function 'void Blinky::blink(int)':
../Core/Blinky.cpp:20:21: error: 'LED_GPIO_Port' was not declared in this scope
20 | HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
| ^~~~~~~~~~~~~
../Core/Blinky.cpp:20:36: error: 'LED_Pin' was not declared in this scope
20 | HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
| ^~~~~~~
../Core/Blinky.cpp:21:12: error: 'delay' was not declared in this scope; did you mean 'del'?
21 | HAL_Delay(delay);
| ^~~~~
| del
make: *** [Core/subdir.mk:19: Core/Blinky.o] Error 1
"make -j8 all" terminated with exit code 2. Build might be incomplete.
10:09:31 Build Failed. 7 errors, 0 warnings. (took 242ms)
Upvotes: 0
Views: 3247
Reputation: 555
CubeIDE isn't really C++ friendly to begin with, so here's a quick step-by-step guide to get started.
File > New > STM32 Project
, it'll open the CubeMX interface, select your STM32 F4 board, and select C++ as Targeted Language
. Then, click on Next, Next again, and finally on Finish.Good, from now on, we have our project ready. Next step now.
rename it as main.cpp
being given we want a C++ project. By default, it'll be generated as main.c, keep an eye on that. While renaming the file, make sure Update references
is checked so CubeIDE does not lost itself in the process.MyCode
into which we add our first class, here both Blinky.h
and Blinky.cpp
.Let's add your code a bit rewritten, check the includes below.
Blinky.h :
#ifndef BLINKY_H_
#define BLINKY_H_
class Blinky {
public:
Blinky();
~Blinky();
void blink(int delay);
};
#endif /* BLINKY_H_ */
Blinky.cpp :
#include "Blinky.h"
#include "stm32f4xx_hal_gpio.h"
#include "stm32f4xx_hal.h"
Blinky::Blinky() {}
Blinky::~Blinky() {}
void Blinky::blink(int delay)
{
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_Delay(delay);
}
main.cpp
and add in the user section includes :/* USER CODE BEGIN Includes */
#include "Blinky.h"
/* USER CODE END Includes */
Click on build, and... Ohoh, error.
Luckily, it's an easy one. We just forgot to tell the compiler where to look for. Right click on the project, Preferences
and we go into the C/C++ Build > Settings
add the new include path. Be careful here, you want to add the class location, here the folder MyCode, into the G++ compiler settings, not into the GCC ones. Click on Add... and type within ../MyCode
. Then we build, and it is fine. Good !
/* USER CODE BEGIN 2 */
Blinky blk;
blk.blink(100);
/* USER CODE END 2 */
You might be tired of those errors for a so simple project and so am I. Let's explain concisely : an undefined error exists when the linker did not find something, here, the class constructor. Because the compilation step succeeded (no error told such as "no definition found"), when all object files are grouped together to create the binary, the linker says "Oh, you might have done your job well before, but I can't figure out where this thing (object or function) is. Help me finding it !".
The solution here is to tell him where to find the missing symbols it's looking for. We thus come back to Project Preferences
and this time, in C/C++ General > Paths and Symbols > Source Location
and we add where our stuff is.
And that's all ! Building C++ stuff within CubeIDE is now working. Enjoy your project.
Upvotes: 2