LLVM equivalent of gcc -D macro definition on commandline
Asked Answered
M

2

25

I am looking for LLVM (or clang) equivalent of gcc's -D flag which enables macro definition at commandline.

Any pointers would be great.

Mera answered 13/3, 2013 at 12:51 Comment(0)
I
35

From clang --cc1 --help:

...
-D <macro>=<value>      Define <macro> to <value> (or 1 if <value> omitted)
...

As a rule of thumb, assume that Clang emulates GCC, unless proven otherwise!

Illiberal answered 13/3, 2013 at 12:58 Comment(3)
Which version of clang are you using ? Mine is 3.3 and I dont see that option when I do clang -cc1 --help.Mera
@mishr: Ah, I seem to have 3.1 where I am right now. It's unlikely that they changed/removed such a fundamental option, though...Illiberal
I tend to agree on the unlikely part, but the option is not shown in help.Mera
M
7

The default clang invocation is a gcc-like compiler driver, supporting the same options as gcc, including -D:

: ~$ cat test/z.c
int foo() {
  return FOOBAR;
}
: ~$ clang -DFOOBAR -E -c test/z.c
# 1 "test/z.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 154 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "test/z.c" 2
int foo() {
  return 1;
}

So if you want to replace gcc, just invoke clang. clang -cc1 invokes the front-end component of clang, not the generic compiler driver.

Medina answered 13/3, 2013 at 13:44 Comment(3)
I am doing that already. Since I am using a makefile, I fire the command make cc=/path/to/clang. I hope that is correct.Mera
@mishr: in general just replacing gcc with clang should work, but there are caveats for specific cases and build sequences. are you getting any particular errors? because -D does work.Medina
Yes -D works, thats why I accepted the answer above. BTW compilation is going fine, linking is not done and thus no executable is generated.Mera

© 2022 - 2024 — McMap. All rights reserved.