c preprocessor to determine project or exe name - c

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?

Related

How to highlight a common header file based on different source module contexts?

I have a project comprised of several source modules and header files. Two of them (s1.c, s2.c) #include the same header file (s3.h). That header file contains conditional compilation construct, based on an externally defined macro:
#ifdef ExtMacro
#define IntMacro 1
#else
#define IntMacro 2
#endif
Now, ExtMacro is defined in s1.c before the #include "s3.h", but is not defined in s2.c.
When opening the header file in the Eclipse editor, the code is parsed for syntax highlighting, and the parts that are excluded from the build are highlighted in gray background.
As you can see, the excluded part of s3.h depends on the context of its inclusion. But Eclipse chooses one of the including modules for the purpose of highlighting the header code.
Is there a way to tell Eclipse to highlight the header code in one context or another?
If you add the header to the list at Preferences -> C/C++ -> Indexer -> "Index all variants of specific headers", then CDT will index both versions of the header.
When you then open the header by following the #include in s1.c, it will show you the version indexed in the context of s1.c. When you open it by following the #include in s2.c, it will show you the version indexed in the context of s2.c.
I don't know of a way to control which version is shown when you open the header without context (e.g. via the Project Explorer).
(Instead of adding the header to the "Index all variants of specific headers" list, you could also just check "Index all header variants". However, I don't recommend this, as it is likely to have an adverse impact of the performance of indexing your project.)
You can set ExtMacro as Symbol in Paths & Symbols. Then you create two build configurations, one where ExtMacro is defined and one where it is not. Then you set the indexer to re-index, when the configuration changes. With the switch of the configuration the indexer will show the correct context.
Then you likely need to exclude the fixed define in s1.c from the indexer. The Eclipse CDT parser generates a preprocessor symbol that you can trigger on and use:
#ifdef __CDT_PARSER__
#else
#define ExtMacro
#endif
This way the symbol ExtMacro is hidden for the CDT parser but not your real compiler and it only picks up the symbol from the build configuration.

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.

Where is UNS_32 defined?

I'm using Doxygen on a client's source code and Doxygen can't find a symbol UNS_32.
The client's code compiles without errors using the GNU ARM compiler.
I have searched the client's code base and can't find the definition of UNS_32.
I searched the GNU ARM source code tree and can't find it either.
So, where is the symbol UNS_32 defined?
*Note: I'm not looking for the meaning. I want the definition so I can put it in my Doxygen configuration file. *
Example usage:
void lpc_heap_init (void *base_addr, UNS_32 heap_size);
It is defined in lpc_types.h
http://code.google.com/p/32bitmicro/source/browse/trunk/src/nxp/lpc17xx/LPC1700CMSIS/Drivers/include/lpc_types.h?spec=svn226&r=226
/** SMA type for 32 bit unsigned value */
typedef uint32_t UNS_32;
You need also to search and headers in paths specified to the compiler with -I options. You can similarly specify these in Doxygen.
My preferred approach is to use Visual Studio, create a "Makefile Project", add the sources and copy all the project's command line defined macros and include paths to the project settings, then let the Intellisense simplify code navigation - it is a simple case of right-clicking the symbol, and selecting "Goto Definition", or using the "Code Definition" window which shows the defining source for any symbol you place the cursor on. You could even set up the project to actiually build the code too.

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

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 ...

Auto generate header files for a C source file in an IDE

I am trying to use Eclipse and NetBeans for programming in C (not C++). Is there a feature/plugin for them which automatically keeps the source and header files in sync?
As in, when I implement a function in the source file, does it automatically insert the correct lines in the header file?
I did look at solutions like lzz, but they are not what I am looking for.
Eclipse CDT allows you to write a prototype in the header file, and automatically add it to the C file.
Instructions
Add function prototype to .h file void foobar()
Select the function name "foobar" (try double clicking)
In the toolbar click Source -> Implement Method
Wizard it up
Thats probably the best you're gonna get out of the box
Agree with approach proposed by Ryu. In C, I would not automatically create declarations in headers. This should be an explicit action making public some symbol from the C module.
However if declaration/implementation are already setup and you want to modify any of them, I imagine that with Eclipse you may want to use Toggle Function Definition in a possible workflow where you copy in clipboard intermediate toggling results and paste them later over the changed declaration or implementation declaration.
Also use rename refactoring intensively when you change things.

Resources