Best way to concatenate #define to "string"

This is a discussion on Best way to concatenate #define to "string" within the C Programming forums, part of the General Programming Boards category; I have some code as such Code: #define THE_PATH "/path/to/somthing" // later on down I do this some_function (THE_PATH "/image.png"); ...

  1. #1
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20

    Question Best way to concatenate #define to "string"

    I have some code as such
    Code:
    #define THE_PATH "/path/to/somthing"
    // later on down I do this
    some_function (THE_PATH "/image.png");
    This is a decent sized app and THE_PATH is something that I dont want to hardcode like this "/path/to/something/image.png"

    It compiles, but with a warning of "traditional C rejects string concatenation." It also works fine in the app. (using GCC 3.2.1)

    My only question is there a more graceful way of performing this and something that wont produce warnings?

    This is for an open source project GrubConf and this change should be making its way to the masses in the next release.

    Thanks for any insight you can provide.
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Why don't you concatenate the strings (Join them together)?
    You can do this with strcat() function, that is an ANSI function.

    Or, you can build your own function to do what you want, and then you call your function, with the string cocatenated, and the compiler won't return warnings.

  3. #3
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    I dont want to do strcat because I want it to be concatentated at build time (which i dont think my example does) for performance considerations
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What options are you compiling with? My version of gcc 3.2.2 IIRC doesn't complain, as it shouldn't, imho.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    I'm using automake/autoconf with no special configure or make options.

    I think -Wall or -Wtraditional will show the warning.

    But my main concern is that older versions of GCC (2.x) will not compile correctly. I will use a strcat or something if I have to but I would prefer to make this work at compile time. THE_PATH can be modified by configure, so hardcoding is not an option.
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use the preprocessor directive to combine strings.

    #define CAT(a,b) a ## b

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

  7. #7
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    This is what it looks like now

    Code:
    #define THE_PATH "/path/tosomething"
    #define CAT(a,b) a ## b
    some_function (CAT (THE_PATH, "/image.png"));
    but I get this warning,
    warning: pasting "PACKAGE_PIXMAPS_DIR" and ""/grubconf-app.png"" does not give a valid preprocessing token

    Did I do something wrong? or should I disregard the warning? Because the program did compile and run succesfully.
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Joe Monti
    Did I do something wrong? or should I disregard the warning? Because the program did compile and run succesfully.
    Code:
    #include <stdio.h>
    #define DOG "hello"
    #define CAT(a,b) a##b
    int main( void )
    {
    	printf("%s", CAT( DOG, " world\n") );
    	return 0;
    }
    This should compile and run without error.

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

  9. #9
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    Yeah .... It compiles and runs without error. I will just disregard the warning.

    Thank you!
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing flags from a DLL?
    By RobotGymnast in forum C++ Programming
    Replies: 17
    Last Post: 10-27-2008, 02:34 PM
  2. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 05:28 PM
  3. Bor to DevC++ prog convert prob
    By kryptkat in forum Windows Programming
    Replies: 16
    Last Post: 09-18-2007, 06:11 AM
  4. 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
  5. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM

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