Search code examples
cudac-preprocessornvcc

Pass preprocessing variable to NVCC for compiling CUDA?


When I compile my CUDA code with NVCC and I have already defined a preprocessing variable in the code, e.g. #define DEBUG_OUTPUT 0, is there a way to overwrite such a variable on the fly when compiling? I tried so specify the NVCC option -DDEBUG_OUTPUT=1 but this doesn't work: It gives me:

warning C4005: 'DEBUG_OUTPUT': Macro-Redefinition


Solution

  • Whatever you specify after -D, it gets defined before processing the input files. However, it does not remove the definitions that occur in the file. So, if you write -DDEBUG_OUTPUT but then you have #define DEBUG_OUTPUT in the file, the latter is a redefinition of the former. To handle that case, you can write in the file:

    //if not specified earlier (e.g. by -D parameter)
    #ifndef DEBUG_OUTPUT
    //set it now to some default value
    #define DEBUG_OUTPUT 0
    #endif
    

    Note, it has actually nothing to do with nvcc. Same behaviour appears in C/C++.