How can I get Eclipse to index code inside #ifdef .... #endif - c

I'm using eclipse to work on some c code and it is not indexing code inside conditional compilation blocks like this:
#ifdef USE_FEATURE_A
int feature_a(...) {
some = code(here);
}
#endif
How can I get eclipse to index the feature_a function?

You could tell eclipse that USE_FEATURE_A is defined. Open your project properties and go to the "C/C++ General->Paths and Symbols" page, under the "Symbols" tab click the "Add" button and put USE_FEATURE_A in the name feild and click OK.
Note: this will cause it not to index any #else sides to your preprocessor stuff... so unless they are all like the one in question you can't AFAIK, but if they are they you're good. (Eclipse contains a C preprocessor that it uses to analyize your code all the stuff above does is essentially the same as adding -DUSE_FEATURE_A to your command line so Eclipse's preprocessor will behave differently from the one in your compiler)

This is an easier and in my opinion more elegant solution to the one selected as the solution:
If someone has the same problem (as I had), this can (now?) easily be solved by going to Window->Preference->C/C++/Indexer and enable "Index all header variants".
Then click Project->C/C++ Indexer->rebuild and clean and build your project. This should resolve all error originating from preprocessor commands.

For what it's worth, getting eclipse to parse conditionally compiled code is much harder to do than would appear at first glance. I found a paper on by IBM from 2007 where they said they will prioritize for the "next release".
Handling Conditional Compilation in CDT's Core

I had this same problem, but the code conditionally eliminated by preprocessing was perfectly valid c code and I wanted it formatted... This was my solution:
1) Global find/replace of #if to #JUNKif
2) Ctrl-Shift-F to reformat the source
3) Another global find/replace of #JUNKif to #if

One way to index code under flag in Eclipse(Kepler) c/c++Editor.
You can enable the compilation flags in Eclipse editor so that code under them can be indexed.
Properties > Preprocessor Include Paths > CDT User settings Entries
Click on ADD and add the Preprocessor Macro and you can specify its value.

Best way I guess is to use the Indexer option : Project Properties>C/C++ General>Indexer.
You can choose Enable project specific settings
I prefer choosing "Use active build configuration" so that all files which are actually built in the project are indexed.
Anyhow you can also choose to index all files in the project even if they are not included in the build ...

Related

Code inside #ifdef SYMBOL [...] #endif is omitted, but SYMBOL is defined - STM32CubeIDE

the Story
I use Symbols to control if a specific piece of Code should be included in the build or not - as most probably everyone working with C/C++ (and others) does. In this particular case I use two symbols, to control if the code running on the Microcontroller is in "Setup"-Mode, or in "EEPROM-Init Mode".
For Both Modes I use Symbols, the Setup Mode Symbol is "SWCONFIG_SETUP" and the EEPROM-Init Symbol is "SWCONFIG_EEPINIT".
The Symbols are defined in the Project Properties of CubeIDE:
The issue is:
both marked lines are skipped in debugging. I'd expect that I could step over/into them when I debug in single step moded, but the debugger just jumps over them, just as if they were disabled and the Symbol SWCONFIG_EEPINIT would not be defined.
I can control that e.g. the code is not executed, because i can not jump into the function with the debugger, and additionaly I can see the Files in the EEPROM were not erased, what would be the purpose of EEP_erase_files();
In the Code both sections are enabled, indicating that the Symbol is defined, otherwise that sections would be greyed out. In an other Code Section, using the #ifdef ... #else ... #endif syntax, the debugger jumps in the greyed out section, indication that it ignores the Symbol.
What I tried to resolve this:
I tried to rebuild/refresh the Index
I deleted the code and re-wrote it.
I deleted the Build Configuration and created it new
I deleted the Symbol in the Project Properties and created it new (using Copy & Paste from the code, to avoid typos)
The Question:
What the heck is going on here? Why is the IDE showing the code section as active, but the Debugger ignores it?
The strange thing is: this workflow allready did it's job!
Solved:
The issue was: i was starting the "Setup" Configuration, not the "SetupEEP" configuration, because for some reason i don't really understand, STM32CubeIDE does not automatically create a Debug configuration, when you create a Build configuration.
I had to create a new Debug Configuration, pointing to the executable in the SetupEEP directory. I just built the "SetupEEP" executable and pressed the debug button, but was not aware, that I started the Debug session with the executable from the "Setup" Build, because i had not created a Debug Configuration for "SetupEEP". This is why the code was omitted: because it was not included in the Build I was running ...
BTW: nice Pitfall, STM!

Is it possible to view FASM preprocessor output?

I'm using fasm to compile a dll that uses macros shipped with fasm, i would like to see what the output is after the preprocessor stage but before the binary stage. Is there any way to see this? I would like to quickly see what is being generated to see if it's Worth getting rid of the dependency on the macro.
Fresh IDE has a feature "Unroll macro" - compile the source, position the caret on the row with the macro invocation and press Ctrl+U or select from the drop down menu "Unroll macro". The preprocessed code will be displayed on the scratch pad window.
If you want the whole preprocessed code at once - use the converting tools located in the FASM package, in the directory tools/ - you need to compile tools/%YOUR_OS%/prepsrc.asm.
But you should always remember, that the reverse side of having so powerful macro engine is that the complex macros are pretty hard to debug.
You could get it by prepsrc. You need to compile tools/%YOUR_OS%/prepsrc.asm by fasm.
fasm WIN32/PREPSRC.ASM
Next you need to get fas file. I do it by fasmw: Run -> Build symbols (for example let's call it file.fas).
Next:
prepsrc file.fas our_preprocessed_code.asm
our_preprocessed_code.asm will contain preprocessed souces.

Eclipse: Compiler defines and Code highlighting

I have an existing C-Project with a pre-written Makefile. I imported the whole thing as a Makefile-Project into Eclipse and it is working fine so far - meaning I can navigate through the code with eclipse and I can build it without problems.
Now I added some Compiler Symbols like NO_ASMto my makefile and updated the code at some points with the according lines of
#ifdef NO_ASM
// Code
#endif
This code is greyed out by my Code-Editor. Now I added the Symbol NO_ASM to
Properties/C/C++ General/Paths and Symbols and rebuilt the indexer - but the according code is still grey. Is there any way to solve this?
Are you sure you added it to all configurations? in all Languages?
You can Try Freshen all files option.
A temporal workaround can something like this:
#ifndef NO_ASM
#define NO_ASM
#endif

c preprocessor to determine project or exe name

I have a C resource file called resources.rc, which contains the following line to specify the icon used for a project
1000 ICON "icon222.ico"
I would like to use this same resource file for several projects using
a pre processor conditional depending on the project..
e.g
#if __PROJECT__ == "myapp.exe"
1000 ICON "icon222.ico"
#endif
#if __PROJECT__ == "myotherapp.exe"
1000 ICON "icon777.ico"
#endif
Is there a standard C macro or definition that could be used to
achieve something like this ?
As far as I known there is no predefined macro carring a project specific value setup by VC.
So just select one yourself like MYPROJECTNAME and #define it differently in each of your projects and then do test this in your rc file as by your posting.
I'm not sure anymore whether VC uses the pre-processor on the rc file automagically or if you need to apply some mods to VC's build process to have it do this.
Update:
To have this feature added using ${EXENAME} in terms of having global (solition wide) settings for VC a way to go might be shown here: Visual c++ 2008: how to have global settings defined in a solution or/and here: Can I pass a preprocessor definition to the resource compiler through the command line?

How to avoid library finding CMakeLists feature

I'm trying to adjust 3rd person code to my needs. This code is provided with CMake config files used to build and install it. There is possibility to choose one of libraries. And in code is often used #ifdef USE_FTD2XX directive. I saw that this is defined in CMamkeFiles.txt file like here:
option(USE_FTD2XX "Use FTDI libFTD2XX instead of free libftdi" ON)
if(USE_FTD2XX)
find_package(libFTD2XX)
endif(USE_FTD2XX)
if(LIBFTD2XX_FOUND)
include_directories(${LIBFTD2XX_INCLUDE_DIR})
add_definitions( -DUSE_FTD2XX )
else(LIBFTD2XX_FOUND)
set(LIBFTD2XX_LIBRARIES "")
endif(LIBFTD2XX_FOUND)
But if I simply use *.c and *.cpp files and I analyse and run it simply from IDE (Codeblocks), how could I set using this library in C++ code instead of in CMake? I'm also sure that I want use always this one so it can be fixed.
Should I simply #define USE_FTD2XX in main file?
You cannot simply #define USE_FTD2XX because you also need specific linker options for this to work (i.e. the library to link with). If the option is OFF in cmake, the specific link options won't be present in the Makefile and most likely you'll have linker errors.
So CMake takes care of everything automatically for you, but you need to re-generate your makefiles each time you want to toggle options on/off.
If only headers were involved and no library to link with (like some parts of the Boost framework), then yeah, defining USE_FTD2XX in your should be enough.

Resources