cancel
Showing results for 
Search instead for 
Did you mean: 

what to include in header and source files

Roman_E
Associate

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.

2 REPLIES 2
Bob S
Super User

You need to include main.h in whatever file needs those pin defines, i.e. in ledfunc.c.

Andrew Neil
Super User

@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: 

https://community.st.com/t5/stm32cubeide-mcus/adding-new-c-source-files-to-my-project-and-navigating-through/m-p/658310/highlight/true#M25925

See also:

https://community.st.com/t5/stm32cubeide-mcus/adding-new-c-source-files-to-my-project-and-navigating-through/m-p/657455/highlight/true#M25847

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.