Questions tagged [conditional-compilation]

Compilation of certain parts of source code will be included/excluded. This can be often reached by pre processing the source code in some way. Including/Excluding parts of the source may be controlled by pre processor keywords.

Using conditional compilation a compiler is able to produce differences in the executable produced based on parameters that are provided during compilation. This technique is commonly used when these differences are needed to run the software on different platforms, or with different versions of required libraries or hardware. Typically a preprocessor, such as the or , is used to conditionally process source files before they are passed to the compiler.

References

728 questions
32
votes
11 answers

Why should #ifdef be avoided in .c files?

A programmer I respect said that in C code, #if and #ifdef should be avoided at all costs, except possibly in header files. Why would it be considered bad programming practice to use #ifdef in a .c file?
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
30
votes
2 answers

C-style conditional compilation in golang

Does golang support #define DEBUG #ifdef DEBUG fmt.Println("Debug message..."); #endif So I can build a debug version with zero runtime overhead?
can.
  • 2,098
  • 8
  • 29
  • 42
28
votes
1 answer

Why is /clr incompatible with /mt and /mtd in Visual Studio?

can anybody please explain for me how and why /clr is incompatible with /mtd ? What is the alternative for this? What happens internally if I use /md or /mdd ? As far as I know we don't combinedly use /clr and /mtd. Can someone explain if there is a…
26
votes
5 answers

Conditional compile-time inclusion/exclusion of code based on template argument(s)?

Consider the following class, with the inner struct Y being used as a type, eg. in templates, later on: template class X{ template struct Y{}; template struct Y{}; }; Now, this example will obviously…
Xeo
  • 129,499
  • 52
  • 291
  • 397
26
votes
3 answers

How to #ifdef by CompilerType ? GCC or VC++

I used #ifdef Win32 for safe calls alike sprintf_s but now I want to build project with MinGW and it's just wrong now. I need to use #ifdef VC++ or somehow like that. Is it possible?
cnd
  • 32,616
  • 62
  • 183
  • 313
25
votes
5 answers

Conditional compilation depending on the framework version in C#

Are there any preprocessor symbols which allow something like #if CLR_AT_LEAST_3.5 // use ReaderWriterLockSlim #else // use ReaderWriterLock #endif or some other way to do this?
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
24
votes
4 answers

Debug Mode In VB 6?

How can I do something similar to the following C code in VB 6? #ifdef _DEBUG_ // do things #else // do other things #end if
Nahum
  • 6,959
  • 12
  • 48
  • 69
23
votes
6 answers

What C preprocessor conditional should I use for OS X specific code?

What C preprocessor conditional should I use for OS X specific code? I need to include a specific library if I am compiling for OS X or a different header if I am compiling for Linux. I know there is __APPLE__ but I don't know if that is a current…
klynch
  • 1,076
  • 2
  • 9
  • 15
22
votes
5 answers

Is it possible to conditionally compile to .NET Framework version?

I can recall back when working with MFC you could support multiple versions of the MFC framework by checking the _MFC_VER macro. I'm doing some stuff now with .NET 4 and would like to use Tuple in a couple of spots but still keep everything else 3.5…
dkackman
  • 15,179
  • 13
  • 69
  • 123
21
votes
3 answers

Using conditional rules in a makefile

I capture the intent of the Makefile in pseudo code, then indicate the issues I have. I'm looking for a Makefile which is more user friendly in a test environment. The correct usage of the Makefile is one of the below. make CATEGORY=parser…
Mike
  • 1,205
  • 3
  • 12
  • 21
20
votes
11 answers

Conditional Java compilation

I'm a longtime C++ programmer, new to Java. I'm developing a Java Blackberry project in Eclipse. Question - is there a way to introduce different configuration sets within the project and then compile slightly different code based on those? In…
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
20
votes
3 answers

Visual Studio conditional project reference based on a constant

For user authorization, I only want to include a specific module for each user. So I configured Conditional Compilation like this TRACE;DEBUG;SAMPLECONSTANT1 and edited the project file like…
19
votes
6 answers

Conditional compilation in Python

How to do conditional compilation in Python ? Is it using DEF ?
user46646
  • 153,461
  • 44
  • 78
  • 84
18
votes
1 answer

Is there a way to enforce correct spelling of features?

Let's assume I have the following feature defined in Cargo.toml: [features] my_feature = [] And the following code lives in src/lib.rs: #[cfg(feature = "my_feature")] fn f() { /* ... */ } #[cfg(not(feature = "my_faeture"))] // <-- Mind the…
Peter Varo
  • 11,726
  • 7
  • 55
  • 77
18
votes
3 answers

Is it safe to use #ifdef guards on C++ class member functions?

Suppose you have the following definition of a C++ class: class A { // Methods #ifdef X // Hidden methods in some translation units #endif }; Is this a violation of One Definition Rule for the class? What are the associated hazards? I suspect if…
1
2
3
48 49