Boolean

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Boolean

1,919 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by richas on Tue Feb 16 14:28:45 MST 2010
I am porting code and the "bool" type is not recognized.  What would be the proper way to add it? 


Rich

Original Attachment has been moved to: 1100122_Balamir.zip

0 Kudos
10 Replies

1,402 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by oingo456 on Wed Sep 01 14:50:20 MST 2010
One answer is to change to NEWLIB, which is C99 compliant, then #include <stdbool.h> just works.

I don't know why it doesn't work with REDLIB.. it is only three macros.  If you want, you can add the following to code using REDLIB  and then it will work (Taken from GCC's stdbool.h )

#define bool    _Bool
#define true    1
#define false   0

To be fair, they don't claim C99 compliance with redlib; but it seems to me it should have worked.
0 Kudos

1,402 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Tue Feb 23 04:57:17 MST 2010
One where the object file was not compiled with that option... People are free to change the option, so this is more of an FYI that if they did, then the binaries would not be compatible.
0 Kudos

1,402 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Tue Feb 23 04:36:47 MST 2010
If I understand correctly, this option is the default. So, what would be a non-default application binary?

Renan
0 Kudos

1,402 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Tue Feb 23 02:04:17 MST 2010
In the documentation (Help->Help Contents), you can see this option is the default:

-fshort-enums
Allocate to an enum type only as  many bytes as it needs for the declared range of possible values. Specifically,  the enum type will be equivalent to the  smallest integer type which has enough room.  [B]Warning:[/B] the -fshort-enums switch  causes GCC to generate code that is not binary compatible with code generated  without that switch. Use it to conform to a non-default application binary  interface.

If you want to use int data types for enums, use -fno-short-enums

Thus, in your example (sizeof(bool)) will return 1.
0 Kudos

1,402 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Mon Feb 22 13:01:48 MST 2010

Quote: annodomini2
Enumerations typically use the local int type, which should theoretically be the native 32bit on the cortex m3.



That's possible, I'm really not sure.

Renan
0 Kudos

1,403 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by annodomini2 on Mon Feb 22 12:55:29 MST 2010

Quote: renan
You could do this too:
enum BOOLEAN                
{
    FALSE = 0,
    TRUE = 1
};        
typedef enum BOOLEAN bool;
That's what I use.

Renan



Enumerations typically use the local int type, which should theoretically be the native 32bit on the cortex m3.

So this implementation may use more RAM, the benefit for the coder being the 'intellisense' can pick up the values.

Of course this depends on the compiler implementation, maybe CodeRed could provide some insight?
0 Kudos

1,403 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Mon Feb 22 06:58:54 MST 2010
You could do this too:
enum BOOLEAN                
{
    FALSE = 0,
    TRUE = 1
};        
typedef enum BOOLEAN bool;


That's what I use.

Renan
0 Kudos

1,403 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by richas on Tue Feb 16 18:17:17 MST 2010
That works, thank you very much.

Rich
0 Kudos

1,403 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by igorsk on Tue Feb 16 17:46:57 MST 2010
bool is a C++ type and is not defined in plain C (I think it might be in C99 though). If you want the native type, you need to compile your code as C++ (e.g. make sure to use .cpp extension). Otherwise something like this should be enough:

typedef unsigned char bool;
#define true 1
#define false 0
0 Kudos

1,403 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by richas on Tue Feb 16 14:44:12 MST 2010
Tried including stdbool.h but it could not find it...........
0 Kudos