Home > OS >  Get a warning with #ifndef foo #def foo #warning foo is n defined #endif
Get a warning with #ifndef foo #def foo #warning foo is n defined #endif

Time:09-05

EDIT : reformulate my question :

In file like delay.h (for avr programming in embedded system) you find this kind of pre-processor define :

#ifndef F_CPU
/* prevent compiler error by supplying a default */
# warning "F_CPU not defined for <util/delay.h>"
# define F_CPU 1000000UL
#endif

It seems quite simple. If nobody defined a value for F_CPU, this will declare 10000000 for it and continue compilation. There is no .c file only a .h file.

Now I have a couple of .c and .h file of my own. And when I try to implement this kind of protection for another value. I get a warning even if I previously declared the value in mu main.c file.

main.c file : 

#define BAUDE_RATE_SPEED 115200
#include "uart_ctrl.h"


void main(){

}

uart_ctrl.h file : 

#ifndef __UART_CTRL_H__
#define __UART_CTRL_H__

#ifndef BAUDE_RATE_SPEED
#define BAUDE_RATE_SPEED 9600
#warning "BAUDE_RATE_SPEED defined at 9600 in uart_ctrl.h"
#endif

void uart_config();

#endif 

uart_ctrl.c file : 

#include "uart_ctrl.h"

void uart_config(){

MCU_REGISTER.UART = BAUDE_RATE_SPEED;

}

build error :

Warning     #warning "BAUDE_RATE_SPEED defined at 9600 in uart_ctrl.h" [-Wcpp]  \uart_ctrl.h    Build   1


Why this rise me a warning ? Is it normal and i don't understand behaviours or I missed something ?

Thanks again for your help.

------------- Previous question

Ok, I didn't exactly how to ask this questions. Let me know if you see a better option than the title I set.

On embedded system is very common to declare the mcu frequency with this statement for 20Mhz frequency.

#define F_CPU 20000000UL

And it's common too, to have this kind of check, in files using it. Let say delay.h :

#ifndef F_CPU
/* prevent compiler error by supplying a default */
# warning "F_CPU not defined for <util/delay.h>"
# define F_CPU 1000000UL
#endif

If I define F_CPU in my main or anywhere before including my delay.h the warning does not appear and the the first declared F_CPU value is used.

Now I'm writing my own library, I'm trying to do the same, with the same structure. For example :

#ifndef UART_BAUD_SPEED
/* prevent compiler error by supplying a default */
# warning "UART_BAUD_SPEED not defined bla bla"
# define UART_BAUD_SPEED 115200
#endif

But it trig warning anyway. If my library is .H file only or if this is couple h/c file.

How can I implement the same warning/error protection in my library ? What i'm missing ?

Thanks in advance for your help.

CodePudding user response:

The compiler compiles each file separately. When it compiles main.c, main.c defines BAUDE_RATE_SPEED before it includes uart_ctrl.h. When it compiles uart_ctrl.c, nothing defines BAUDE_RATE_SPEED before uart_ctrl.h, so it is not defined and it gives the warning.

When you run the compiler it has an option to define things automatically before compiling the file, which might be where F_CPU comes from. It would be typical to add -DBAUDE_RATE_SPEED=9600 to the compiler command for all files in the project, instead of defining it separately in each file. If you are using an IDE, you may have to figure out where to enter this in the IDE.

CodePudding user response:

Ok, I found where is my miss-understanding.

as @user253751 said, each files is compiled separately. But this only apply to file with couple .h/.c file. Because the delay.h file is header only file, it is never compiler alone. Only included. So the warning never triggered.

Thanks for your help.

  • Related