Thread: Shell command as a preprocessing token

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    98

    Shell command as a preprocessing token

    This is a C preprocessor question: How to convert a shell command which outputs a string with whitespaces within it into a valid preprocessor token?

    I have this program:
    Code:
    int main()
    {
       printf("%s\n", GCCVERSION);
    }
    And I try to compile it using:
    % gcc -DGCCVERSION="\"`gcc --version`\"" testver.c
    Unmatched ".
    FYI:
    % gcc --version
    gcc (GCC) 3.4.4
    Copyright (C) 2004 Free Software Foundation, Inc. ....

    I appreciate your help.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It's getting confused by the newline characters that are output as part of the gcc --version command. Here's one solution:

    Code:
    $ gcc -DGCCVERSION="\"$(gcc --version|tr '\n' ' ')\""  junk1.c
    
    $ ./a
    gcc (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125) Copyright (C) 2004 Free Software Found
    ation, Inc. This is free software; see the source for copying conditions.  There is NO warranty; not
     even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    If you want to see what is going on during the preprocessing stage, use the -E flag on the main gcc call:

    Code:
    $ gcc -DGCCVERSION="\"$(gcc --version|tr '\n' ' ')\""  -E junk1.c
    
    ...
    int main()
    {
       printf("%s\n", "gcc (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125) Copyright (C) 2004
    Free Software Foundation, Inc. This is free software; see the source for copying conditions.  There
    is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  ");
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    98
    Does not work in my system:
    % gcc -DGCCVERSION="\"$(gcc --version | tr '\n' ' ' ) \"" test.c
    Illegal variable name.

    My gcc is 3.4.4.
    And I thought CPP was confused by the blank between "gcc" and "(GCC)", and the blank between "(GCC)" and "3.4.4".
    Last edited by hzmonte; 10-26-2005 at 07:48 PM. Reason: correct the gcc version - 3.4.4

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    It depends on what shell you're using. What shell are you using? The above works on bash/zsh, but fails on csh/tcsh. It is likely you are using tcsh.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    98
    tcsh.
    Can someone suggest something that can work in tcsh? Many thanks.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    98
    OK, since nobody had any suggestion about making it work with tcsh, I am giving up on that. My new question is: How can I incorporate a (bash) shell command in a GNU Makefile?
    Same as before, my program is this:
    Code:
    int main()
    {
       printf("%s\n", GCCVERSION);
    }
    It is OK for me to do this:
    Code:
    bash-2.05$ gcc -DGCCVERSION="\"$(gcc --version |head -1)\"" testver.c
    bash-2.05$ a.out
    gcc (GCC) 3.4.4
    But if I write a Makefile like this:
    Code:
    a.out: testver.c
            gcc -DGCCVERSION="\"$(gcc --version |head -1)\"" testver.c
    It won't work:
    Code:
    bash-2.05$ make 
    gcc -DGCCVERSION="\"\"" testver.c
    bash-2.05$ a.out
    What's wrong? Thank you.

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Make does shell interpolation differently, this should work:
    Code:
    a.out: testver.c
        gcc -DGCCVERSION="\"$(shell gcc --version | head -1)\"" testver.c
    And I expect nobody offered a way in tcsh because nobody uses tcsh

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Parsing and Tokens (strtok)
    By readerwhiz in forum C Programming
    Replies: 6
    Last Post: 04-22-2002, 10:57 AM
Website Security Test