cancel
Showing results for 
Search instead for 
Did you mean: 

Add source file

Fannyrsy
Associate II

Hi everyone,

I'm trying to add a new file Alarm.c to my project. I added it in the Core>Src folder but I want to use it in a custom_app.c file that is located in another folder (STM32_WPAN>APP) as I use BLE. I included it to this file but when I compile, it doesn't find it in this file. How can I do to declare Alarm.c in one location and all my project's folder can find it?

Thank you

5 REPLIES 5
AScha.3
Chief III

Hi,

> I added it in the Core>Src folder

How - just copy in ?

To let the IDE know, some (.c or .h) file now part of your project, you have to use : import.

So right click on the folder you want it to be, -> import -> from file sytem -> next ...

AScha3_0-1708421997769.png

 

If you feel a post has answered your question, please click "Accept as Solution".

Thank you for your answer. I simply created a new .c file in the Core>Src folder. I am new to STM32CubeIDE and C programmation. I would like to create a .c file with different functions in it that are used in main.c and custom_app.c files. How can I do this ?

folders.png

Almost same: right click on the folder you want it to be, -> new -> Source file

+ give name (no spaces, no special characters) , like : my_new.c  

Thats it.

If you feel a post has answered your question, please click "Accept as Solution".

@Fannyrsy wrote:

I would like to create a .c file with different functions in it that are used in main.c and custom_app.c files. How can I do this ?


@AScha.3 covered how to create the .c file.

You will also need to create a .h "header" file - use the same approach:

AndrewNeil_0-1708432674215.png

The header file contains extern declarations of the things you want to be shared with other source files.

To use it, those other source files will need to #include the header.

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

To be sure that the compiler will be able to find the header, it must be in a folder that's included in the Include Paths.

The auto-generated ST code tends to have separate folders for source (Src) and header (Inc) files; eg,

AndrewNeil_1-1708433251262.png

 

This is a matter of style that you may or may not wish to follow...

 

TDK
Guru

> I would like to create a .c file with different functions in it that are used in main.c and custom_app.c files. How can I do this ?

If you want to use functions in other files, you need to declare them inside of a header file or before you  use them. Simply including the source file in your application isn't going to do this.

In your c file, define the functions:

void my_function_1() {
    ...
}

void my_function_2() {
    ...
}

In your h file, declare it:

void my_function_1();

void my_function_2();

In your other c files, include the header with the declarations.

#include "my_functions.h"
If you feel a post has answered your question, please click "Accept as Solution".