cancel
Showing results for 
Search instead for 
Did you mean: 

Adding new c source files to my project and navigating through them

Sraman
Associate II

Hi all, I am fairly new in using STM32CubeIDE. I am working with three peripherals (uart1, uart2 and CAN) as also a timer interrupt in the STM32F103C8. I have written all the code in the main.c file. It is working but the coding is a bit jumbled up. For better readability, I want to put different functions of the three peripherals and the timer in different source files and call them from the main function. I am adding new c source files in the src folder but I have trouble in accessing the functions placed in these files from the main.c file. Can anybody please guide me in this matter?

1 ACCEPTED SOLUTION

Accepted Solutions

As @SofLit said, you use header files (.h) to provide the declarations of the things you want to share.

This is standard C stuff - nothing specific or special to CubeIDE:

https://c-faq.com/decl/decldef.html 

 

eg, file.h provides declarations:

 

extern int   variable1;

extern char  variable1;

extern float variable1;

void function_1( void );

 

file.c provides definitions (or "implementations"):

 

int   variable1;

char  variable2;

float variable3;

void function_1( void )
{
    // your code here
}

 

Now, for example, your main.c can access things from file.c by #including file.h:

 

#include "file.h"

int main( void )
{
   int   local_variable1 =   2 * variable1;  // use the variables from file.c
   char  local_variable2 =   3 + variable2;
   float local_variable3 = 2.3 / variable1;

   function_1();                             // call the function from file.c
   :
   :

 

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

 

Here is a pictorial summary:

AndrewNeil_0-1722589349504.png

 

#DeclDef 

View solution in original post

9 REPLIES 9
SofLit
ST Employee

You need to export these functions in .h files.

Then include these .h files in your main.c.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Dear SofLit,

Can you please share some example codes or some reading material?

Rim LANDOLSI
ST Employee
Hello @Sraman   and welcome to the community,
 
To link functions in main.c to separate C files, you can follow this example steps:
  • Add the source file (somm.c) to the src folder:

 

int sommm(int a, int b){

	return(a+b);
}

 

  • Add header file (somm.h) under Inc folder:
#ifndef INC_SOM_H_
#define INC_SOM_H_

int sommm(int,int);

#endif /* INC_SOM_H_ */
  •  In main.c, include the header file:
 #include "som.h"
 
Then use the function you want
 
 
Hope this helps!
 
Thanks,
Rim.

One of the examples you can refer to is BSP example in the STM32CubeF1 from here.

If you look at the main.h file already included in main.c, there are many .h files related to the BSP source code:

#include "stm32f1xx_hal.h"
#include "stm3210e_eval.h"
#include "stm3210e_eval_lcd.h"
#include "stm3210e_eval_sd.h"
#include "stm3210e_eval_audio.h"
#include "stm3210e_eval_nor.h"
#include "stm3210e_eval_sram.h"
#include "stm3210e_eval_nand.h"
#include "stm3210e_eval_serialflash.h"
#include "stm3210e_eval_tsensor.h"

The source files are located here.

So the APIs defined in .c files are exported in .h files which in turn included in main.h which finally included in main.c. So all the functions defined in these drives will be seen in main.c file.

Don't forget to add the include path(s) in your IDE.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

As @SofLit said, you use header files (.h) to provide the declarations of the things you want to share.

This is standard C stuff - nothing specific or special to CubeIDE:

https://c-faq.com/decl/decldef.html 

 

eg, file.h provides declarations:

 

extern int   variable1;

extern char  variable1;

extern float variable1;

void function_1( void );

 

file.c provides definitions (or "implementations"):

 

int   variable1;

char  variable2;

float variable3;

void function_1( void )
{
    // your code here
}

 

Now, for example, your main.c can access things from file.c by #including file.h:

 

#include "file.h"

int main( void )
{
   int   local_variable1 =   2 * variable1;  // use the variables from file.c
   char  local_variable2 =   3 + variable2;
   float local_variable3 = 2.3 / variable1;

   function_1();                             // call the function from file.c
   :
   :

 

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

 

Here is a pictorial summary:

AndrewNeil_0-1722589349504.png

 

#DeclDef 

Sraman
Associate II

Thanks everybody. As @Andrew Neil said, it is standard C stuff and I managed to do the coding.

You're welcome.

One thing I omitted to mention is that it's good to also #include your file.h header in your file.c source file.

So, in file.c:

 

#include "file.h"   // contains the declarations

                    // here are the definitions:
int   variable1;

char  variable2;

float variable3;

void function_1( void )
{
    // your code here
}

 

This means that the Compiler, when it compiles file.c, gets to see both the declaration in file.h and the definitions in file.c - and, thus, can warn you if there's any discrepancy.

Yeah, I am already doing this.