cancel
Showing results for 
Search instead for 
Did you mean: 

What is the correct way of using classes in main.c

Chao
Senior

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

 

Chao_0-1707061972606.png

Anyone knows any workaround?

 

Regards

Chao

 

13 REPLIES 13

Sorry I have to come back to this post for a new issue in compiling C++ code.

The C++ code compilation is all right now in my previous test project which is my first CubeIDE project. Then I was trying to use that method to do the same thing in the example project (Nucleo-STM32F767ZI) :  LwIP_HTTP_Server_Netconn_RTOS

However, I found unexpectedly I don't get the option of "Convert to C++" when right click on this project in Project Explorer.

When I  right click on other projects, I could see this option or option of "Convert to C" for the one I already converted to C++.

I also did a new import of this example, and the result does not change.

Any solution for this?

Regards

Chao

I did this example import again more carefully, and found the following:

Chao_0-1707186630434.png

It looks like that it's not an STM32CubeIDE version example, is not compatible with CubeIDE,  and is not configurable in CubeIDE (actually there is no .ioc file),   so is it possible to convert this example from other toolchain/IDE (say MDK-ARM) to CubeIDE?

It would be highly appreciated if someone could share a link of this conversion.

Regards

Chao

TDK
Guru

> so is it possible to convert this example from other toolchain/IDE (say MDK-ARM) to CubeIDE?

See here:

https://wiki.st.com/stm32mpu/wiki/How_to_move_from_SW4STM32_to_STM32CubeIDE

Can also just try "Import ac6 System Workbench for STM32 Project" without the extra steps.

If you feel a post has answered your question, please click "Accept as Solution".
Pavel A.
Evangelist III

In CubeIDE, do "Import"....  then in the dialog select "Import STM32CubeIDE example".

This option is slightly better than "Import ac6 System Workbench..." as the import tool has more clues. Internally, of course, it will use the SW4STM variant of the example, so all advices on importing from SW4STM and fixes after  the import, apply. See also here.