24

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

Any pointers would be great.

shrm
  • 1,112
  • 2
  • 8
  • 20

2 Answers2

33

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!

yegor256
  • 102,010
  • 123
  • 446
  • 597
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Which version of clang are you using ? Mine is 3.3 and I dont see that option when I do `clang -cc1 --help`. – shrm Mar 13 '13 at 13:10
  • @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... – Oliver Charlesworth Mar 13 '13 at 13:32
  • 1
    I tend to agree on the unlikely part, but the option is not shown in help. – shrm Mar 13 '13 at 13:45
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.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 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. – shrm Mar 13 '13 at 13:55
  • @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. – Eli Bendersky Mar 13 '13 at 13:57
  • 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. – shrm Mar 13 '13 at 14:00