Reputation: 31675
I have in my source code:
// foo.cpp
struct foo
{
foo() {}
#ifdef I_WANT_THIS_FEATURE
void bar() {}
#endif
};
In my Makefile I have
foo.o: foo.cpp
g++ -c -DI_WANT_THIS_FEATURE foo.cpp -o foo.o
This compiles fine from the command line as well as with the external builder that I have created in Eclipse (which basically defines a few environment variables and calls make
) and I can call foo::bar()
.
However, in the Eclipse CDT source code editor, the part where I define foo::bar()
has a grey background (suggesting that foo::bar()
would not be included in the build) and code completion on objects of type foo
does not suggest bar()
as a method that can be called.
How can I define the I_WANT_THIS_FEATURE
macro in an Eclipse CDT makefile project with custom makefile so that it is known to the source code editor and the code completion?
Upvotes: 4
Views: 6339
Reputation: 52887
Eclipse makes this rather confusing, since there are multiple places to set this, and the settings are coupled, but here's how it works:
Note: if you've followed my instructions below, or the other answers here, and it still won't work, check out my more-extensive troubleshooting section on my website here.
Here's my preferred way to do it.
In this example we will set the following defines at the Eclipse project level (for its indexer and builder) rather than in your source code.
#define ARDUINO 1000
#define AVR
#define __AVR_ATmega328__
If you were defining them at the command-line when manually building a gcc
or g++
project, the above #define
s would look like this (search for -Dmacro
in the man gcc
pages for details):
-DARDUINO=1000 -DAVR -D__AVR_ATmega328__
So, do the following in your Eclipse project. My project here is called "Arduino 1.8.13" (see full screenshot of this a couple images below):
Right-click on your project in the "Project Explorer" pane on the left --> Properties --> C/C++ General --> Paths and Symbols --> Symbols tab --> select either GNU C or GNU C++ --> click the Add button at the top-right --> type ARDUINO
for name and 1000
for value --> BE SURE TO CHECK THE 2 BOXES FOR Add to all configurations and Add to all languages (unless you don't want those behaviors) --> click OK.
Repeat this step for all defines, as follows. Be sure to check the boxes for Add to all configurations and Add to all languages (unless you don't want those behaviors) for each one:
ARDUINO
, Value: 1000
AVR
, Value: (leave empty)__AVR_ATmega328__
, Value: (leave empty)Here's a screenshot showing the first one. Notice all the highlighted sections to click or pay attention to:
Once you've done this for all macros you wish to define (ex: the 3 above), you will see the following:
When done adding all macros, click Apply or Apply and Close. When asked, choose YES to re-index the entire project:
If you didn't click YES, you may manually trigger the project to be reindexed by right-clicking on it in the Project Explorer and going to --> Index --> Rebuild.
Update: see also my more-extensive troubleshooting section on my website here.
If your settings/macros don't seem to be getting applied, and your code still shows sections blacked-out, indicating the macros for those sections are false or undefined, you may need to do or check the following:
Upvotes: 0
Reputation: 31675
Found it: Project -> Properties -> C/C++ General -> Paths and Symbols
Choose the Symbols tab and Add... a new Symbol with Name I_WANT_THIS_FEATURE
and a Value of 1
.
Upvotes: 3
Reputation: 127
In addition to Oswald's answer:
If you have several build configurations, the default behavior of the Eclipse Indexer seem to be that it always uses the first build configuration.
In my case the define was only defined in the 3rd build configuration, so the solution provided by Oswald did not help.
To change this globally, select Window -> Preferences -> C/C++ -> Indexer. Choose Use active build configuration
You could also choose to override the global settings in the project settings under Project -> Properties -> C/C++ General -> Indexer and select Enable project specific settings followed by selecting Use active build configuration.
After this, the solution provided by Oswald should work:
Project -> Properties -> C/C++ General -> Paths and Symbols
Choose the Symbols tab and Add... a new Symbol with Name I_WANT_THIS_FEATURE and a Value of 1.
Upvotes: 5
Reputation: 111
Using -D with almost every compiler and just supplying a name like -DI_WANT_THIS_FEATURE defines the symbol I_WANT_THIS_FEATURE with a value of 1.
The eclipse indexer/editor apparently does not know that, so:
#if I_WANT_THIS_FEATURE
this code is marked inactive in editor,
but will be seen by compiler and cause error
#endif
where:
#ifdef I_WANT_THIS_FEATURE
this code is marked active in editor
#endif
So, this is really a problem with eclipse not knowing that default value for a symbol defined through -D is 1.
Upvotes: 0