This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Problem using: gcc -DREL="test" x.c


>>>>> "Shikhar" == Shikhar Sarkar <shikhar@utstar.com> writes:

    Shikhar> Hi, I will highly appreciate if someone can tell me how to
    Shikhar> pass a string constant in compiler option -D. Say, if I give
    Shikhar> -Dmacro=5, it works, but if I give -Dmacro="test", it fails in
    Shikhar> the compilation process. How can I specify string as a command
    Shikhar> line option.

    Shikhar> Shikhar Sarkar Senior Research Engineer UTStarcom Inc.  New
    Shikhar> Jersey

    Shikhar> Following are the details:

    Shikhar> Works correctly: gcc -DNUM=5 x.c [assignment to an integer
    Shikhar> works] Does not work: gcc -DREL="test" [assignment to string
    Shikhar> does not work]

    Shikhar> Test program: main () { char rel[100] = REL;

    Shikhar>  puts(rel); printf("%d", NUM); }

    Shikhar> Compilation: $ g++ -DNUM=5 -DREL="test" rel.c rel.c: In
    Shikhar> function `int main()': rel.c:5: `test' undeclared (first use
    Shikhar> this function) rel.c:5: (Each undeclared identifier is
    Shikhar> reported only once rel.c:5: for each function it appears in.)

Hi,

the problem is that the command line option -DREL="test" you pass to gcc is
interpreted by the shell, and so are the double quotes. The quotes are
removed by the shell. What you need to do is either to escape the quotes
and pass -DREL=\"test\" to gcc or e.g. -DREL='"test"'.

Alternatively, you may define a macro:

#define stringify(x) #x

and in your main function:

  char rel[100] = stringify (REL);

Then, you may pass -DREL=test (which is really equivalent to -DREL="test")
to gcc.

Hope this helps.

Claudio


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]