How can I check presence of flag at compile time?
I am using the -LITTLE
flag for choosing little endian calculation and
-BIG
for big endian calculation in my project whil开发者_运维问答e compiling.
#ifdef LITTLE
{
// i'm using i for operating one loop
}
#endif
/* If the system is big-endian, store bytes in array as forward order */
#ifdef BIG
{
// using i for loop
}
#endif
like
gcc -LITTLE my_c_file.c
I want to check if user hasn't given any of flag at compile time then compilation does not takes place and give an error.
How can I do that?
I think you mean gcc -DLITTLE
.
You can use something like:
#if !defined(LITTLE) && !defined(BIG)
#error either LITTLE or BIG must be defined
#endif
At a guess, you might also want:
#if defined(LITTLE) && defined(BIG)
#error only one of LITTLE or BIG must be defined
#endif
Of course, it's better if you can write code that doesn't care about the endianness of the maching that you are running on and avoid the whole mess.
Try
#if !defined(LITTLE) && !defined(BIG)
#error "Either LITTLE or BIG has to be defined"
#endif
You can detect the byte sex of the platform automatically. See boost/detail/endian.hpp.
精彩评论