Importing HAL libraries C++ files.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-12-04 5:10 AM
Hi,
I have created an empty C++ in STM32CubeIDE to create an a project with my own structure. I don't want to use or have the code generation tool on my project.
The problem that I have is that I can't import the HAL libraries from a C++ files since when I compile it fails, I get undefined reference for all the functions that come form the HAL.
I have already added the HALlibrary files to the project structure and added the path for C and C++ in the project settings.
I think that I'm doing something wrong when integrating the c libraries from the HAL into de C++ code.
from my main file I call a c function in a cpp function and from there I call my GPIO.h library that is cpp. Inside this library I call the HAL functions.
Does someone know what I am doing wrong?
Below are a summarize of my files.
Thank you.
main.c
#include <stdint.h>
#include "App.h"
int main(void)
{
//Start app
InitApp();
while(1);
}
App.h
#ifndef APP_H_
#define APP_H_
#ifdef __cplusplus
extern "C" {
#endif
void InitApp();
#ifdef __cplusplus
}
#endif
#endif /* APP_H_ */
App.cpp
#include "App.h"
#include "GPIO.h"
GPIO gpio;
void Run()
{
// Main application loop
while (1)
{
}
}
void InitApp(void)
{
// Initialize basic HW configuration
gpio.InitGPIO()
Run();
}
GPIO.h
#ifndef GPIO_H_
#define GPIO_H_
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "stm32g4xx_hal_gpio.h"
#include "stm32g4xx_hal_rcc.h"
#ifdef __cplusplus
}
#endif
class PPGPIO
{
public:
GPIO();
void InitGPIO();
};
#endif /* GPIO_H_ */
GPIO.cpp
#include "GPIO.h"
#include "stm32g473xx.h"
void GPIO::InitGPIO (void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
// GPIO Ports Clock Enable
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
//Configure GPIOD to inputs with no pull-ups
GPIO_InitStruct.Pin = GPIO_PIN_All;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
Solved! Go to Solution.
- Labels:
-
STM32CubeIDE
-
STM32G4 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-12-05 12:41 AM
Hi,
Nevermind, I found 3 problems that where causing this:
- I was missing the Symbols for USE_HAL_DRIVER and STM32G473xx in Properties > C/C++ General > Paths and Symbols > Symbols Tab
- I had to add the HAL library path to Source Location that was also missing in n Properties > C/C++ General > Paths and Symbols > Source Location
- Lastly, I was missing the system_stem32g4xx.c on my projec.
I hope this is useful for someone else when creating a clean project.
Thank you TDK for your help.
Regards,
Alex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-12-04 6:59 AM
The only file you should #include is "stm32g4xx_hal.h". This file will pull in everything it needs based on the definitions such as STM32F427xx (or similar) and your included HAL modules which are defined in "stm32g4xx_hal_conf.h".
Most (all?) HAL includes have an "extern C" guard inside of them so you don't need to (and probably shouldn't) put that in your header before including them.
So at the top of GPIO.h, you only need:
#include "stm32g4xx_hal.h"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-12-04 8:01 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-12-04 9:03 AM
Here's one error I get when compiling your project:
In file included from ../Src/PPADC.cpp:8:0:
../Inc/PPADC.h:20:5: error: 'ADC_HandleTypeDef' does not name a type; did you mean 'UART_HandleTypeDef'?
ADC_HandleTypeDef hadc1;
^~~~~~~~~~~~~~~~~
UART_HandleTypeDef
Okay, so it can't see ADC definitions.
Go to your stm32g4xx_hal_conf.h file and see the following:
/*#define HAL_ADC_MODULE_ENABLED */
Okay, so you do not have the ADC module enabled. Makes sense that it can't see ADC stuff.
Uncomment that line.
Now it compiles, or at least mostly compiles. Still one error, but that's unrelated to HAL:
Src/main.o: In function `main':
C:/Users/tdkostk/Desktop/PP/Debug/../Src/main.c:31: undefined reference to `InitApp'
To fix that, change your PPApp.h header to uncomment the stuff you commented.
#ifdef __cplusplus
extern "C" {
#endif
void InitApp();
#ifdef __cplusplus
}
#endif
Now it compiles:
arm-none-eabi-objdump -h -S PP.elf > "PP.list"
arm-none-eabi-objcopy -O binary PP.elf "PP.bin"
arm-none-eabi-size PP.elf
text data bss dec hex filename
828 12 1568 2408 968 PP.elf
Finished building: default.size.stdout
Finished building: PP.bin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-12-04 12:07 PM
Hi,
Thank you very much. I had commented the extern to test since you told me that it wouldn't be needed.
I don't know how I missed the HAL_ADC_MODULE_ENABLED of you notice, I had already enabled the necessary ones for the GPIO and so no.
Anyway, again, Thank you!!
Have a nice weekend :)
Regards,
Alex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-12-05 12:11 AM
Hi again,
I have checked again and I already got to the point you suggest. Doing what you suggested compiles but only while you don't call any of the functions on the C++ classes like initADC(); If you do that, then it does gives you the same errors as before.
Attached is the example. Could you please check it?
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-12-05 12:41 AM
Hi,
Nevermind, I found 3 problems that where causing this:
- I was missing the Symbols for USE_HAL_DRIVER and STM32G473xx in Properties > C/C++ General > Paths and Symbols > Symbols Tab
- I had to add the HAL library path to Source Location that was also missing in n Properties > C/C++ General > Paths and Symbols > Source Location
- Lastly, I was missing the system_stem32g4xx.c on my projec.
I hope this is useful for someone else when creating a clean project.
Thank you TDK for your help.
Regards,
Alex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-12-05 6:33 AM
