Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a string literal in gcc command line?

In gcc command line, I want to define a string such as -Dname=Mary, then in the source code I want printf("%s", name); to print Mary.
How could I do it?

like image 713
richard Avatar asked Mar 09 '10 17:03

richard


Video Answer


1 Answers

Two options. First, escape the quotation marks so the shell doesn't eat them:

gcc -Dname=\"Mary\" 

Or, if you really want -Dname=Mary, you can stringize it, though it's a bit hacky.

#include <stdio.h>  #define STRINGIZE(x) #x #define STRINGIZE_VALUE_OF(x) STRINGIZE(x)   int main(int argc, char *argv[]) {     printf("%s", STRINGIZE_VALUE_OF(name)); } 

Note that STRINGIZE_VALUE_OF will happily evaluate down to the final definition of a macro.

like image 73
Arthur Shipkowski Avatar answered Sep 21 '22 11:09

Arthur Shipkowski