Reputation: 783
I'm using Keil
to code for STM32 microcontrollers. In main.h
file, I use this code:
#if defined __has_include //Keil recognizes this preprocessor. If it was unavailable, Keil would have marked it as gray
#if __has_include("Debugg.h")
#include "Debugg.h"
#endif
#endif
This, checks if the Debugg.h
file exists. Let's imagine I have a #define
in Debugg.h
file:
#define DEBUGGING 1
Now, I expect that in main.h
, the DEBUGGING
is defined. Again main.h
:
#if defined __has_include //Keil recognizes this preprocessor. If it was unavailable, Keil would have marked it as gray
#if __has_include("Debugg.h")
#include "Debugg.h"
#endif
#endif
#ifdef DEBUGGING
#define MAIN_DEBUG 1 //This line runs
#else
#define MAIN_DEBUG 0 //Keil IDE mark this line as gray, meaning "unavailable code"
#endif
But, whenever I try to print MAIN_DEBUG
or use it, it is 0
!
NOTE:
It seems that Keil IDE recognizes the __has_include
preprocessor, but the compiler DOES NOT; because when I comment out these lines:
//#if defined __has_include
#if __has_include("Debugg.h")
#include "Debugg.h"
#endif
//#endif
I get these errors:
compiling main.c...
..\Library\main.h(5): error: #59: function call is not allowed in a constant expression
#if __has_include("Debugg.h")
..\Library\main.h(5): error: #31: expression must have integral type
#if __has_include("Debugg.h")
..\Library\main.c: 0 warnings, 2 errors
I also use the default ARM compiler version 5
. Any suggestions?
Thanks.
Upvotes: 0
Views: 621
Reputation: 141523
Any suggestions?
Do not use non-portable extensions like __has_include
. Build systems are used to detect information about environment, like available headers.
For example, CMake build system has check_include_file
that you can check if an include file exists. Then if the include file Debugg.h
(why upper+lower case mix?) exists, then add a macro HAS_DEBUGG_H
to compilation and if that macro is defined, then include the header.
a libX.c, a libX.h and libX_conf.h file. the checking for Debugg.h is in config file. If it was available, it checks for the prints of a specific thread. If not, debugging is deactivated
That should be done with a macro, not with "detecting header file". There should be a macro, called like LIBX_DEBUG_ENABLE
and if defined, libX
should output debugging information. I.e. it is a user configuration setting done with a macro, not with deleting a file.
Upvotes: 1