Skip to main content
Associate II
Feb 20, 2024 9:25 AM UTC
Question

Add source file

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

    This topic has been closed for replies.

    5 replies

    AScha.3
    Super User
    Feb 20, 2024 9:40 AM UTC

    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 on " Best Answer ".
    FannyrsyAuthor
    Associate II
    Feb 20, 2024 9:49 AM UTC

    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

    AScha.3
    Super User
    Feb 20, 2024 12:06 PM UTC

    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 on " Best Answer ".
    TDK
    Feb 20, 2024 2:11 PM UTC

    > 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""."