2024-02-20 01:25 AM
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
2024-02-20 01:40 AM - edited 2024-02-20 01:40 AM
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 ...
2024-02-20 01:49 AM
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 ?
2024-02-20 04:06 AM
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.
2024-02-20 04:42 AM - edited 2024-02-20 04:47 AM
@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:
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,
This is a matter of style that you may or may not wish to follow...
2024-02-20 06:11 AM
> 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"