Thread: using environment variable: gcc -D vs. #define

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    using environment variable: gcc -D vs. #define

    Hi y'all! I have some defines like this:

    Code:
    #define DIR "path"
    #define FILE "file"
    That I use like this (eg):

    Code:
    fopen(DIR"/"FILE, "r");
    No problem. However, I want to include another define that is passed to the compiler on the command line or in a makefile, taken from an environment variable, eg:

    Code:
    export WROOT=/some/path
    gcc -DROOT=$WROOT
    But different rules seem to apply because of the shell and special characters such as '/'. I could do this:

    Code:
    gcc -D'ROOT=/some/path'
    Which prevents the shell screwing up on "/", but I cannot include an environment variable in single quotes for the same reason -- the shell will then just pass '$WROOT' thru literally.

    Anyone know of a way around this?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could just use the char *envp[] arguments to main?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quzah View Post
    You could just use the char *envp[] arguments to main?
    Quzah.
    Sounds like a good suggestion but this is a library/module, so there is no main
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by MK27 View Post
    Code:
    gcc -D'ROOT=/some/path'
    Have you tried the below; just a guess from a Windows user.
    Code:
    gcc -DROOT="/some/path"

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    getenv function?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The "way around this" depends on your command shell, not gcc.

    The problem is, every shell has slightly distinct handling of quote (and double quote) characters. Since you are looking to have a #define specified on the gcc command line that expands to something with double quotes, you need to carefully read the documentation for your shell.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Code:
    gcc "-DMACRO=VALUE"
    That virtually always works for me, but I only use a couple of different shells.

    I'd still recommend you alter your use of the macro to expand the string inline with the quotes.

    Soma

    Code:
    #if !defined(DIR)
    #define DIR /some/path
    #endif
    
    #define WRAP2(X) #X
    #define WRAP1(X) WRAP2(X)
    #define WRAP(X) WRAP1(X)
    
    #define ACTIVEDIR WRAP(DIR)
    
    #include <stdio.h>
    
    int main()
    {
    	printf(ACTIVEDIR);
    	return(0);
    }

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Because you're making a string literal, the quotes are part of the macro expansion, so you need them to be passed to gcc. Shells will eat your quotes because they're special characters. Solution: escape the quotes. Any shell worth its salt will allow you to escape with \, so you'd use:
    Code:
    gcc -DROOT=\"$WROOT\"
    This passes actual quotation marks on the command-line, allowing gcc to correctly create a string literal ($WROOT will still be expanded by the shell).

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bayint Naung View Post
    getenv function?
    Then I can't use it like a define.

    Quote Originally Posted by cas View Post
    Shells will eat your quotes because they're special characters. Solution: escape the quotes. Any shell worth its salt will allow you to escape with \, so you'd use:
    Code:
    gcc -DROOT=\"$WROOT\"
    This passes actual quotation marks on the command-line, allowing gcc to correctly create a string literal ($WROOT will still be expanded by the shell).
    Whew! That's the ticket. It's actually for use with apxs, which compiles (via gcc) and installs apache modules. I thought it was doing it's own strange husking, but what it was because the command gets passed thru the shell twice, ie what works is:

    Code:
    apxs -D MY_DEF=\\\"$MY_ENV_VAR\\\"
    Heh. Thanks all!
    Last edited by MK27; 03-13-2011 at 09:24 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DHCP Discover
    By Xandux in forum C Programming
    Replies: 4
    Last Post: 03-06-2010, 12:41 AM
  2. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 05:11 AM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM
  5. Help getting multiple keypresses in a DOS compiler
    By Nongan in forum Game Programming
    Replies: 2
    Last Post: 04-01-2005, 10:07 PM

Tags for this Thread