2024-02-04 07:59 AM
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
Solved! Go to Solution.
2024-02-04 01:05 PM - edited 2024-02-04 01:06 PM
It's not compiling C++ files. Right click project in Project Explorer, select Convert to C++.
2024-02-04 08:23 AM
Classes don't exist in C. You can rename it to main.cpp, or you can put your code in other.cpp and call it from there.
2024-02-04 09:09 AM
Thanks.
I changed the main.c to main.cpp and the linker reports error:
The line 99 of startup_stm32f767zitx.s is a jump instruction (jump to main):
/* Call the application's entry point.*/
bl main
Regards
Chao
2024-02-04 09:13 AM
@Chao To use C++ successfully, one has to know it )) Just to "have plenty of code" does not count. Find a course?
2024-02-04 09:24 AM - edited 2024-02-04 10:37 AM
Put the definition of main within an extern "C" { }.
Or maybe you're not compiling C++ files.
2024-02-04 11:01 AM
I put an extern "C" { } in two ways for test:
1. surround the main() { }:
extern "C" {
main()
{
... ...
}
}
2. surround the whole main.cpp except for #include <OutputPort.h> that declares a C++ class
But none of them works.
The following is a build after a clean:
I wonder why the compilation of OutputPort.cpp and main.cpp is absent.
Is it possible that something is incorrect in toolset setting?
In NXP's IDE, I got this after I have made a change in main.cpp:
and the following are the contents of main.cpp in my NXP project:
Regards
Chao
2024-02-04 01:05 PM - edited 2024-02-04 01:06 PM
It's not compiling C++ files. Right click project in Project Explorer, select Convert to C++.
2024-02-04 09:50 PM
Great!
It works now, g++ is used at last.
The current method is to call MyMain() function that is in MyMain.cpp instantiating classes.
Thank you so much
Chao
2024-02-04 09:53 PM
The current method is to call MyMain() function in main.c, and the MyMain() function is in MyMain.cpp that instantiates classes.
2024-02-05 01:54 AM
Use this button to properly post source code:
To get that extra row of icons, press this button: