Skip to content

[BUG] true/false always redefined if stdbool.h and Arduino.h are both included when using C. #1570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xxxajk opened this issue Sep 10, 2013 · 2 comments
Labels
Component: Core Related to the code for the standard Arduino API

Comments

@xxxajk
Copy link
Contributor

xxxajk commented Sep 10, 2013

Arduino.h always defines true and false, the trouble occurs when you use C, and include stdbool.h. Arduino.h should simply include stdbool.h instead of defining these for C code, and only define them if using C++.

Details: this seems to happen only for C code, not C++ or a sketch. I have situations where I write a library with mixed C and C++. This causes GCC to emit redefinition warnings.

@matthijskooijman
Copy link
Collaborator

Arduino.h always defines true and false, the trouble occurs when you use C, and include stdbool.h. Arduino.h should simply include stdbool.h instead of defining these for C code, and only define them if using C++.
You say to only define them if using C++, but AFAIU they are language keyword in C++, so there is no need to define them (if you do, you'll be using integers instead of the actual true and false keywords, which will also work, but is not so nice).

So I think that we should just replace the true and false definitions with an #include <stdbool.h> and be done with it. For C, that will make true, false and bool available, for C++ that will just use the C++ keywords (stdbool.h contains a check for C vs C++).

matthijskooijman added a commit to matthijskooijman/Arduino that referenced this issue Feb 19, 2014
In C++, true and false are language keywords, so there is no need to
define them as macros. Including stdbool.h in C++ effectively changes
nothing. In C, true, false and also the bool type are not available, but
including stdbool.h will make them available.

Using stdbool.h means that we get true, false and the bool type in
whatever way the compiler thinks is best, which seems like a good idea
to me.

This also fixes the following compiler warnings if a .c file includes
both stdbool.h and Arduino.h:

	warning: "true" redefined [enabled by default]
	 #define true 0x1

	warning: "false" redefined [enabled by default]
	#define false 0x0

This fixes arduino#1570 and helps toward fixing arduino#1728.

This only changed the AVR core, the SAM core already doesn't define true
and false (but doesn't include stdbool.h either).
@ffissore ffissore added the New label Feb 27, 2014
@cmaglie cmaglie removed the New label Feb 27, 2014
@cmaglie
Copy link
Member

cmaglie commented Apr 20, 2014

Fixed with #1877.

@cmaglie cmaglie closed this as completed Apr 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Core Related to the code for the standard Arduino API
Projects
None yet
Development

No branches or pull requests

4 participants