#define string question

This is a discussion on #define string question within the C Programming forums, part of the General Programming Boards category; Hello, It's been years since i've used C++ so i'm trying to get the cobwebs out. i'm writing a program ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    #define string question

    Hello, It's been years since i've used C++ so i'm trying to get the cobwebs out.

    i'm writing a program in C, using a makefile and compiling it with gcc.
    using the -D command i'm using things like #ifdef _DEBUG print debuging messages.


    In my makefile i have a value called PREFIX which is /usr or /usr/local
    I would like to pass that to my program so it knows where it's files are located. but i can't seem to extract the string from the #define.

    If i pass it a command like -D pi=3.1415 i can in the program call int someint = 3*3*pi
    but i can't get it to work with strings.

    I'm assuming it probably can't be done. so how do i at compile time pass this prefix so my program knows where the files it's looking for are?

    Thanks
    Kevin A

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    I am just guessing, so this may not be what you want..

    Code:
    #ifdef USR
    const char *dir = "/usr"
    #endif
    Then on your command line you could do:

    Code:
    -DUSR
    Of course you can get fancier than that by putting the conditional parts into your makefile, so that you have different targets which can conditionally define different parts of your code, so that you can 'make' one part or some other.
    Last edited by kermit; 07-11-2010 at 05:25 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    gcc -D 'STR="usr/bin"' ...

    printf("%s\n",STR);

    From man gcc:

    If you are invoking the preprocessor from a shell or shell-like
    program you may need to use the shell’s quoting syntax to protect
    characters such as spaces that have a meaning in the shell syntax.

    If you wish to define a function-like macro on the command line,
    write its argument list with surrounding parentheses before the
    equals sign (if any). Parentheses are meaningful to most shells,
    so you will need to quote the option. With sh and csh,
    -D’name(args...)=definition’ works.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Thanks, adding a '' around the whole statement worked.

    I hate it when it's something you should have known.

    Kevin A

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RS232 connection set up
    By cs342 in forum C Programming
    Replies: 25
    Last Post: 05-26-2010, 04:57 AM
  2. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 03:27 PM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 04:18 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21