preprocessor

Why include guards?

倖福魔咒の 提交于 2019-11-26 16:47:18
Include guards, as defined here , are used to prevent loading the same code twice at compilation. Why can't my compiler (GCC) detect that it is loading the same code twice and have a sensible default behaviour? Simply because you might have wanted the compiler to load that file twice. Remember, that #include simply loads a file and puts its contents in the place of the directive. This file might be a header file, but may be useful and frequently used piece of source code as well. Most modern compilers react to #pragma once doing exactly what you want them to. Remember though, that this is a

Xcode / iOS: How to determine whether code is running in DEBUG / RELEASE build?

假装没事ソ 提交于 2019-11-26 15:37:16
I am making an app that processes sensitive credit card data. If my code is running in debug mode I want to log this data to the console and make some file dumps. However on the final appstore version (ie when it is running in release mode) it is essential all of this is disabled (security hazard)! I will try to answer my question as best I can; so the question becomes 'Is this solution path the right or best way to do it?' // add `IS_DEBUG=1` to your debug build preprocessor settings #if( IS_DEBUG ) #define MYLog(args...) NSLog(args) #else #define MYLog(args...) #endif Damo Check your project

How would you do the equivalent of preprocessor directives in Python?

限于喜欢 提交于 2019-11-26 15:23:43
问题 Is there a way to do the following preprocessor directives in Python? #if DEBUG < do some code > #else < do some other code > #endif 回答1: There's __debug__ , which is a special value that the compiler does preprocess. if __debug__: print "If this prints, you're not running python -O." else: print "If this prints, you are running python -O!" __debug__ will be replaced with a constant 0 or 1 by the compiler, and the optimizer will remove any if 0: lines before your source is interpreted. 回答2: I

#define in Java

久未见 提交于 2019-11-26 14:30:18
I'm beginning to program in Java and I'm wondering if the equivalent to the C++ #define exists. A quick search of google says that it doesn't, but could anyone tell me if something similar exists in Java? I'm trying to make my code more readable. Instead of myArray[0] I want to be able to write myArray[PROTEINS] for example. Andrzej Doyle No, because there's no precompiler. However, in your case you could achieve the same thing as follows: class MyClass { private static final int PROTEINS = 0; ... MyArray[] foo = new MyArray[PROTEINS]; } The compiler will notice that PROTEINS can never, ever

Add preprocessor macro to a target in xcode 6

梦想的初衷 提交于 2019-11-26 13:05:14
问题 Probably this is pretty simple, but I can\'t find a way to define a preprocessor macro for a target in Xcode 6. 回答1: I've done a screenshot to show where it is in Xcode, because it's easier :) Select project file Select the target you want Go to Build Settings Search for 'preprocessor' Add your preprocessor macro either for Debug, Release, or both. 回答2: In Xcode 9 you have to add a preprocessor macros to Project, not Target. Also don't forget to add "D" as the firs letter. For example,

CUDA and nvcc: using the preprocessor to choose between float or double

时光怂恿深爱的人放手 提交于 2019-11-26 12:43:51
问题 The problem : Having a .h, I want to define real to be double if compiling for c/c++ or for cuda with computing capability >= 1.3. If compiling for cuda with computing capability < 1.3 then define real to be float. After many hours I came to this (which does not work ) # if defined(__CUDACC__) # warning * making definitions for cuda # if defined(__CUDA_ARCH__) # warning __CUDA_ARCH__ is defined # else # warning __CUDA_ARCH__ is NOT defined # endif # if (__CUDA_ARCH__ >= 130) # define real

Can gcc output C code after preprocessing?

这一生的挚爱 提交于 2019-11-26 12:13:52
I'm using an open source library which seems to have lots of preprocessing directives to support many languages other than C. So that I can study what the library is doing I'd like to see the C code that I'm compiling after preprocessing, more like what I'd write. Can gcc (or any other tool commonly available in Linux) read this library but output C code that has the preprocessing converted to whatever and is also readable by a human? mipadi Yes. Pass gcc the -E option. This will output preprocessed source code. tpdi cpp is the preprocessor. Run cpp filename.c to output the preprocessed code,

How do I override a Python import?

淺唱寂寞╮ 提交于 2019-11-26 12:11:15
问题 I\'m working on pypreprocessor which is a preprocessor that takes c-style directives and I\'ve been able to make it work like a traditional preprocessor (it\'s self-consuming and executes postprocessed code on-the-fly) except that it breaks library imports. The problem is: The preprocessor runs through the file, processes it, outputs to a temporary file, and exec() the temporary file. Libraries that are imported need to be handled a little different, because they aren\'t executed, but rather

Razor view engine, how to enter preprocessor(#if debug)

泄露秘密 提交于 2019-11-26 12:04:47
I am writing my first razor page today, can't figure out how to enter #if debug #else #endif How can i enter preprocessor in razor? Shawn Wildermuth I just created an extension method: public static bool IsDebug(this HtmlHelper htmlHelper) { #if DEBUG return true; #else return false; #endif } Then used it in my views like so: <section id="sidebar"> @Html.Partial("_Connect") @if (!Html.IsDebug()) { @Html.Partial("_Ads") } <hr /> @RenderSection("Sidebar", required: false) </section> Since the helper is compiled with the DEBUG/RELEASE symbol, it works. This is built in to HttpContext : @if

Managing highly repetitive code and documentation in Java

。_饼干妹妹 提交于 2019-11-26 12:02:06
问题 Highly repetitive code is generally a bad thing, and there are design patterns that can help minimize this. However, sometimes it\'s simply inevitable due to the constraints of the language itself. Take the following example from java.util.Arrays : /** * Assigns the specified long value to each element of the specified * range of the specified array of longs. The range to be filled * extends from index <tt>fromIndex</tt>, inclusive, to index * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex=