2

In Xcode can I use ## in a macro?

In MSVC I can write:

#define FOO(_var) int foo##_var## = 1

    FOO(bar);
    foobar++;

On the Mac (edit: compiling with GCC) the same code gives me the error "Pasting "foobar" and "=" does not give a valid preprocessing token. Is ## not supported in xcode?

1 Answer 1

10

Concatenation is supported in GCC and Clang. Xcode isn't a compiler; if you're posting errors like this, check what version of GCC, LLVM-GCC or Clang ("LLVM compiler") you're using because their behavior can differ.

You're trying to make = part of an identifier (i.e., create a variable called foobar=) which I don't think is what you want.

Try #define FOO(_var) int foo##_var = 1 instead.

Incidentally, Clang gives a somewhat better error message:

foo.c:4:5: error: pasting formed 'foobar=', an invalid preprocessing token
    FOO(bar);
    ^
foo.c:1:32: note: instantiated from:
#define FOO(_var) int foo##_var## = 1
                               ^
Sign up to request clarification or add additional context in comments.

1 Comment

That did it, I had extra ## which compiled fine in MSVC but not under GCC. Thanks Nicholas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.