What is the correct way of using classes in main.c
Hi,
I have plenty of embedded code that was written in a mixture of C/C++ with target of NXP processors, now I am going to port it onto STM32 targets, I wrote a mini class OutputPort as a test in CubeIDE:
OutputPort.h:
#include "stm32f767xx.h"
class OutputPort
{
public:
OutputPort(GPIO_TypeDef* gpio, uint16_t gpioPin);
virtual ~OutputPort();
private:
GPIO_TypeDef* gpio;
uint16_t gpioPin;
public:
void Toggle();
void Set();
void Reset();
};
OutputPort.cpp:
#include "OutputPort.h"
#include "stm32f7xx_hal.h"
OutputPort::OutputPort(GPIO_TypeDef* gpio, uint16_t gpioPin)
{
this->gpio = gpio;
this->gpioPin = gpioPin;
}
OutputPort::~OutputPort() { }
void OutputPort::Toggle() { HAL_GPIO_TogglePin(gpio, gpioPin); }
void OutputPort::Set() { HAL_GPIO_WritePin(gpio, gpioPin, GPIO_PIN_RESET); }
void OutputPort::Reset() { HAL_GPIO_WritePin(gpio, gpioPin, GPIO_PIN_SET); }
main.c :
/* USER CODE BEGIN Includes */
#include <OutputPort.h>
/* USER CODE END Includes */
/* USER CODE BEGIN 2 */
Port* Led1 = new Port(GPIOB, GPIO_PIN_0);
/* USER CODE END 2 */
The compiler reports: error: unknown type name 'class'
Then I change the main.c to main.cpp as I did with NXP processor which are also ARM Cortex,
it looks like the compilation is OK, but the linker reports error:
Core/Startup/startup_stm32f767zitx.s:99: undefined reference to `main'
The line 99 of startup_stm32f767zitx.s is a jump instruction (jump to main):
/* Call the application's entry point.*/
bl main

Anyone knows any workaround?
Regards
Chao
