How do I detect a custom configuration? - wpf

I have a WPF application with three custom configurations. The are as follows:
Debug
Deploy_Local
Deploy_Beta
Deploy_Live
Obviously I can use a precompiler directive to detect Debug:
#if DEBUG
// Debug code
#else
// Non-debug code
#endif
How can I do this with the the remaining three configurations?

I believe this was answered somewhat in: Will #if RELEASE work like #if DEBUG does in C#?
Basically, you do need to also define custom symbols at the project level (in the build tab). So if you name your configuration MY_CONFIG, you will have to add the MY_CONFIG symbol to all the project that you want to handle that configuration specifically. You can then do the same construct as:
#if MY_CONFIG
Console.WriteLine("MY_CONFIG");
#endif
Hope this helps,
Eric.

what's wrong with:
#if DEBUG
#else
#if Deploy_Local
// and so on
#endif
#endif

I figured this out on my own. There is a "Conditional Compilation Symbols" in the "Build" tab of the project's settings. It's at least there in WPF applications. I added the precompiler symbols for each of my configurations therein.
I really hate giving the Answer to myself. Sigh! So can anyone elaborate on this?

Related

how solve problem on download and debug using IAR?

whenever I generate my code with STM32cubeMX on Iar embedded workbench V8.5
this error is still present
Fatal Error [Pe035]: #error directive: "Unknown target."
The error is unrelated to either downloading or debugging. It is a build error.
The #error is a pre-processor directive in the code. If you double-click on one of those errors it will no doubt take you to the offending code. It is not possible from the image to direct you to the exact cause (you should post the "build Log" text in its entirety, rather then an image of the "build" tab), but will no doubt be code similar to (elided):
#if defined(STM32F405xx)
#include "stm32f405xx.h"
#elif defined(STM32F415xx)
#include "stm32f415xx.h"
#elif
...
#elif defined(STM32F423xx)
#include "stm32f423xx.h"
#else
#error "Unknown Target" <<<< HERE - no target macro has been defined.
#endif
The point being that the HAL code supports multiple STM32 variants and it is necessary to set a macro identifying your target so that the appropriate part specific code will be built.
If you created your project using STM32CubeMX or from the IAR IDE, there will be some place in the configuration where you can specify your target. (in the case of CubeMX I don't think you can do much of anything until you have selected the target). Failing that there will be some place to define command-line build macros in the build configuration.

How can I compile two version of my code in IAR Embbedded Workbench

I have two version of code and I need to switch them as work need to compile each one while keeping two version on an IAR project. I find something like "compile switch" but I don't know how is it doing. Is there anyone tell me a keyword or an advice that can I search?
You can use C preprocessor #define feature to toggle between code versions and use IAR EWARM project's Defined Symbols feature to enable a list of #defines in a specific header file (for example: defines.h) that will be included in all C files.
defines.h
#if defined(PROD_VERSION)
#define SOFTWARE_VERSION_PRODUCT ("1.0-release")
//...whetever specific #defines meant for the release version, for example...
//#define ENABLE_RF_STUB
#define USE_SERIAL_CTS_RTS
#elif defined(TEST_VERSION)
#define SOFTWARE_VERSION_PRODUCT ("1.0-test")
//...whetever specific #defines meant for the test version, for example...
#define ENABLE_RF_STUB
#define USE_SERIAL_CTS_RTS
#elif defined(DEBUG_VERSION)
#define SOFTWARE_VERSION_PRODUCT ("1.0-debug")
//...whetever specific #defines meant for the debug version, for example...
#define ENABLE_RF_STUB
//#define USE_SERIAL_CTS_RTS
#endif
in rf.c
#include "defines.h"
void rfInit(void)
{
#ifndef ENABLE_RF_STUB
//init RF here
#endif
}
In serial.c
#include "defines.h"
CPU_BOOLEAN isCtsRts()
{
#ifdef USE_SERIAL_CTS_RTS
return HAL_SERIAL.isCtsRts();
#else
return DEF_TRUE; //bypass CtsRts check
#endif
}
In your project option > C/C++ Compiler > Preprocessor > Defined symbols: add PROD_VERSION if you want the release version, or add TEST_VERSION if you want the test version or add DEBUG_VERSION if you want the debug version.
You can only choose one of the three configurations above only as IAR will only compile one version via the project compilation. Unless you can create a batch build script to allow building all the three versions under different output files created with three different project setups.
IAR has a configuration in toolbar Project > Edit_Configuration
It makes you set version "switches" via set these tool and it is possible to set preprocessor command for each setup.

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

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

How do I generate an error or warning in the C preprocessor?

I have a program that must be compiled only in DEBUG mode. (testing purpose)
How can I have the preprocessor prevent compilation in RELEASE mode?
Place anywhere:
#ifndef DEBUG
#error "Only Debug builds are supported"
#endif
For reference: Diagnostics
C provide a #error statement, and most compilers add a #warning statement. The gcc documentation recommends to quote the message.
Maybe something more sofisticated, but it is only copy&paste of previous solutions. :-)
#ifdef DEBUG
#pragma message ( "Debug configuration - OK" )
#elif RELEASE
#error "Release configuration - WRONG"
#else
#error "Unknown configuration - DEFINITELY WRONG"
#endif
P.S. There is also another way how to generate a warning.
Create an unreferenced label like
HereIsMyWarning:
and don't reference it. During compilation, you will get a warning like
1>..\Example.c(71) : warning C4102: 'HereIsMyWarning' : unreferenced label
You can use a error directive for that. The following code will throw an error at compile time if DEBUG is not defined:
#ifndef DEBUG
#error This is an error message
#endif
If you simply want to report an error:
#ifdef RELEASE
#error Release mode not allowed
#endif
will work with most compilers.
For GCC and Clang (and probably any compiler that supports the _Pragma feature) you can define a macro:
#if ! DEBUG
#define FIX_FOR_RELEASE(statement) _Pragma ("GCC error \"Must be fixed for release version\"")
#else
#define FIX_FOR_RELEASE(statement) statement
#endif
You can use this macro for temporary hacks, for example to get around code that a co-worker hasn't written yet, to make sure you don't forget to fix it once you want to release a build to the public. Either
FIX_FOR_RELEASE()
// Code that must be removed or fixed before you can release
or
FIX_FOR_RELEASE(statement that must be removed or fixed before you can release);
In Code::Blocks, if you don't want the Release mode, you can delete the Release mode. To do this, click on the Project menu, select Properties..., and in the Build targets tab you can click on Release and then click on the Delete button. Deleting the Release mode only does it for the current project, so you can still use it in other projects.
Otherwise, if you really want to use the preprocessor, you can do this:
#ifdef RELEASE
#error "You have to use the Debug mode"
#endif

Resources