preprocessor-directive

#if preprocessor directive for directives other than DEBUG

你。 提交于 2019-11-29 15:59:46
问题 I know that I can use preprocessor directives to check for Debug/Release by doing this: #if DEBUG //debug mode #elif //release mode #endif but what about checking for other configurations, like Test. In VB you can do this: #If CONFIG = "Release" Then 'Release mode #ElseIf CONFIG = "Test" Then 'Test mode #ElseIf CONFIG = "Debug" Then 'Debug mode #End If So, my question is in C#, how can I check for Test mode? I have some code that I want to execute if I'm in Debug AND Test, but not in Release

Create multiple versions of a project in Visual Studio using Build Configurations

南笙酒味 提交于 2019-11-29 07:17:33
I neeed to create multiple versions of my project using configuration just like we do with #define , #if , #endif . The down side of using these preprocessor directives is that I need to define symbols in almost every file of the project but I want to handle this thing by my build configurations. I am not even sure if Build Configurations will help me to do this. What I want is if I create a configuration with name "Development" and other with name "QA", my code would look like: if #Development or if $QA Kindly guide me towards achieving this. Steve Configuration Manager exist for this reason.

Preprocessor macro expansion to another preprocessor directive

你。 提交于 2019-11-29 02:17:39
Initially I thought I needed this, but I eventually avoided it. However, my curiosity (and appetite for knowledge, hum) make me ask: Can a preprocessor macro, for instance in #include "MyClass.h" INSTANTIATE_FOO_TEMPLATE_CLASS(MyClass) expand to another include, like in #include "MyClass.h" #include "FooTemplate.h" template class FooTemplate<MyClass>; ? I believe that cannot be done, this is because the pre-processor is single pass . So it cannot emit other preprocessor directives. Specifically, from the C99 Standard (6.10.3.4 paragraph 3): 3 The resulting completely macro-replaced

Making something both a C identifier and a string?

爷,独闯天下 提交于 2019-11-28 18:53:31
Say you want to generate a matched list of identifiers and strings enum { NAME_ONE, NAME_TWO, NAME_THREE }; myFunction(NAME_ONE, "NAME_ONE"); myFunction(NAME_TWO, "NAME_TWO"); myFunction(NAME_THREE, "NAME_THREE"); ..without repeating yourself, and without auto-generating the code, using C/C++ macros Initial guess: You could add an #include file containing myDefine(NAME_ONE) myDefine(NAME_TWO) myDefine(NAME_THREE) Then use it twice like: #define myDefine(a) a, enum { #include "definitions" } #undef myDefine #define myDefine(a) myFunc(a, "a"); #include "definitions" #undef myDefine but #define

Force the compiler to ignore some lines in the program

两盒软妹~` 提交于 2019-11-28 18:33:06
Suppose that I have 10,000 lines of C++ code. 200 lines of this code are for testing purpose (for example, check the program and show an error message). Is there an way in C++ to ignore or consider some lines of the code (maybe with preprocessor keywords)? Short answer: Use macros and #ifdef checking. For example: #ifdef MY_CONTROL_MACRO ... #endif the code within this scope will only be compiled if you already defined the MY_CONTROL_MACRO macro. More stuff: To define such a macro, you can Add #define MY_CONTROL_MACRO to your code. Or, For VS, add MY_CONTROL_MACRO to Project > Properties > C/C

Is #define banned in industry standards?

牧云@^-^@ 提交于 2019-11-28 17:19:21
问题 I am a first year computer science student and my professor said #define is banned in the industry standards along with #if , #ifdef , #else , and a few other preprocessor directives. He used the word "banned" because of unexpected behaviour. Is this accurate? If so why? Are there, in fact, any standards which prohibit the use of these directives? 回答1: First I've heard of it. No; #define and so on are widely used. Sometimes too widely used, but definitely used. There are places where the C

C++ compile time macros to detect windows os

旧巷老猫 提交于 2019-11-28 12:28:44
Are there any C++ compile time macros which exists to detect which Windows OS the code is being compiled on. I basically want to support certain functions only on Win7. So I am interested in doing something like this #if <os_macro> = WIN7 // This function would do something valid only on Win7 builds. bool myfunction { // do something here } #else // This function would typically return false, since its not supported on OS below win7 bool myfunction { return false; } #endif Is there any other better way to do this? The OS that it's getting compiled on is not all that important; what matters

Try statement in Cython for cimport (for use with mpi4py)

て烟熏妆下的殇ゞ 提交于 2019-11-28 11:42:34
Is there a way to have the equivalent of the Python try statement in Cython for the cimport? Something like that: try: cimport something except ImportError: pass I would need this to write a Cython extension that can be compiled with or without mpi4py. This is very standard in compiled languages where the mpi commands can be put between #ifdef and #endif preprocessor directives. How can we obtain the same result in Cython? I tried this but it does not work: try: from mpi4py import MPI from mpi4py cimport MPI from mpi4py.mpi_c cimport * except ImportError: rank = 0 nb_proc = 1 # solve a

Why would somebody use an #if 1 C preprocessor directive?

十年热恋 提交于 2019-11-28 10:53:11
I am looking through some C source code and I don't understand the following part #if 1 typedef unsigned short PronId; typedef unsigned short LMId; # define LM_NGRAM_INT #else typedef unsigned int LMId; typedef unsigned int PronId; # undef LM_NGRAM_INT #endif Why would someone do #if 1 ? Isn't it true that only the first block will ever be processed? Yes.. Only the first block will be processed --- until someone changes the 1 to a 0. Then the other block will be compiled. This is a convenient way to temporary switch blocks of code in and out while testing different algorithms. So that one can

Does the program execution always start from main in C?

风格不统一 提交于 2019-11-28 10:36:21
Must program execution start from main, or can the starting address be modified? #include <stdio.h> void fun(); #pragma startup fun int main() { printf("in main"); return 0; } void fun() { printf("in fun"); } This program prints in fun before in main . The '#pragma' command is specified in the ANSI standard to have an arbitrary implementation-defined effect. In the GNU C preprocessor, '#pragma' first attempts to run the game 'rogue'; if that fails, it tries to run the game 'hack'; if that fails, it tries to run GNU Emacs displaying the Tower of Hanoi; if that fails, it reports a fatal error.