2026-01-16 9:15 AM - last edited on 2026-01-16 9:57 AM by Andrew Neil
I generated project with CubeIDE for Nucleo F411RE board with defaults.
Now I want to create my own function void LED_blink(int blink_num);
So, I created ledfunc.h and ledfunc.c files.
This is my ledfunc.h file:
#ifndef INC_LEDFUNC_H_
#define INC_LEDFUNC_H_
void LED_blink(int led_number_blink);
#endif /* INC_LEDFUNC_H_ */and this if my ledfunc.c file:
void LED_blink(int led_number_blink) {
for (int i = 0; i < led_number_blink; i++) {
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
HAL_Delay(100);
}
}and I want to use LED_blink(2) function in main.c
( which was generated by CubeIDE )
When I try to build code, I'm getting errors LD2_GPIO_Port undefined
Yes, those defined in main.h
Where (in which file ) I need to include #include "main.h" ?
I need proper way where to add #include
Edited to apply source code formatting - please see How to insert source code for future reference.
2026-01-16 9:18 AM
You need to include main.h in whatever file needs those pin defines, i.e. in ledfunc.c.
2026-01-16 10:03 AM - edited 2026-01-16 10:19 AM
@Roman_E wrote:those defined in main.h
If you want to use them in your ledfunc.c file, their definitions need to be available in your ledfunc.c file.
The definitions are in main.h so, as @Bob S said, you will need to #include main.h in your ledfunc.c file
This is standard C stuff - nothing to special with ST or CubeMX or TouchGFX.
PS:
It's also good practice to #include your ledfunc.h header in your ledfunc.c source file - that way, the compiler can check that your LED_blink() function prototype in the .h file matches the implementation in the .c file.
See:
See also: