Defining macro with commas in its value


I have e.g. the following program:

#include <cstdio>

int main()
    constexpr int arr[] = {ARR_VALUES};
    constexpr int arr_size = sizeof(arr) / sizeof(*arr);

    for(int i = 0; i < arr_size; i++) printf("i=%d g=%d\n", i, gpu_idxs[i]);

    return 0;

Now I want to compile it using nvcc:

nvcc -DARR_VALUES=0,1,2,3 -o program.x

The compilation fails with

<command-line>: error: macro names must be identifiers

Using e.g. g++, this macro definition works just fine:

g++ -DARR_VALUES=0,1,2,3 source.cpp -o program.x

I have found a CMake issue regarding this nad found that escaping the commas and using quotes works:

nvcc -DARR_VALUES="0\,1\,2\,3" -o program.x

However, this does not work with g++ now.

Is there any way to make this work that is compatible?

According to nvcc docs (if I understand it right), -D should work exactly like using a #define. gcc docs contain the exact same sentence.

In the cmake issue it was discussed that this is an nvcc issue. Is it an issue or expected behavior?

btw, I am using

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Jun_13_19:16:58_PDT_2023
Cuda compilation tools, release 12.2, V12.2.91
Build cuda_12.2.r12.2/compiler.32965470_0

Thanks for any help,

PS: My real use case is to use preprocessor macros to define CPU-GPU mapping in a multi-gpu system. I don’t want to hardcode that into the source code, but rather into an environment-initialization script.