This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

cannot get the variable value from main.c

Hi, 

I'm new to C language.

In my main.c

extern float BatteryLvl_Volts;

// Use ADC to get BatteryLvl_Volts

float get_BatteryLvl_Volts(void)
{
    return BatteryLvl_Volts;
}

In my Battery.c

#include "main.h"

void battery_level_indicator(float BatteryLvl_Volts)
{
    if( 3.3 < BatteryLvl_Volts ){
        LED_ON(GREEN);       // Turn on Green LED
    }
}

My Question

The IDE will give a warning: No such file or directory. How to solve this issue, please? I need to get BatteryLvl_Volts from main.c. 

Thanks! 

  • Hello,

    I'm new to C language.

    Thank you for clarifying this - this makes it much easier for us to help you since we know that you are new to C. 

    In my Battery.c

    You do not need to include main.h - because main.c includes all other files, not the other way around. If you need to pass a value from main.c to another file, you can either implement that variable as a global variable (but this can get messy for larger programs), or you can pass it as a parameter to the function that will make use of it, for example.
    In the specific code snippets you share, if you first remove the extern keyword for the variable, the variable is then a global variable - you may then use it in any one of your functions. Please see this tutorial for a more in-depth explanation. However, if you use the variable multiple places it could be hard to keep track of everyone accessing it, and I therefore instead recommend that you always pass it as a parameter (either pass-by-value if the function should not change the variable. or pass-by-reference if the function should be able to change it) instead, to make it easier for yourself in the long run.

    Furthermore, you should initialize any global variables, so that you do not relay on the implicit declaration and that you may know if it is ready to use or not (i.e if it is initialized to the value 0, and then always have non-zero values following the first update, you can know that the variable is ready use when it is non-zero, for example).

    In my main.c

    You might be using the extern keyword wrong in your main.c. Please have a look at this blogpost for a more detailed explanation of the extern keyword. In essence, the extern keyword is used to tell the compiler that the variable will come from elsewhere, not that you intend to use it elsewhere. 

    Also, what is your intention with the get function? Is it just to keep the code clean? A global variable will be accessible from anywhere in your program. get functions are usually used to retrieve variables that you do not have access to directly (but it is fine to create a get function to access any variable, as this can help you clean up the program by not having all variables be globally accessible).

    The IDE will give a warning: No such file or directory. How to solve this issue, please? I need to get BatteryLvl_Volts from main.c. 

    Please post the entire error message that you get. The "No such file or directory" error is a very common C error, and usually means that you have not included the proper source files to your project, or that you have not told your compiler where to find the files you are trying to include in the project, for example.

    Please do not hesitate to ask if any part of my answer is unclear, or if you have further questions!

    Best regards,
    Karl

  • StevenW807 said:
    Thank you so much, Karl. Crystal clear!

    No problem at all, I am happy to hear that you found my comment useful! :) 

    Please do not hesitate to open another ticket if you should encounter any other issues or questions in the future.

    Good luck with your development!

    Best regards,
    Karl

Related