Thread: #redefine??

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

    #redefine??

    I'm trying to create a simple patch for a build tree that I didn't write. I need to change a hardcoded (#define) value to an int which can be adjusted during execution. I have checked all references to the variable in the source files and it is treated as an int when passed around, so I don't see a problem.

    However, when I change the original definition (in a header file, nb) from "#define THISNUM 12" to "int ThisNum 12" (and replaced THISNUM with ThisNum everywhere to remain conventional), I get a bunch of "multiple definition" build errors (then the linker fails) from object files that use the sources (I'm not familiar with the make/build process on a whole tree of files).

    What can I do?

    ps. as a hack I added "extern" but still no dice...
    Last edited by MK27; 04-09-2009 at 10:00 AM.
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Mucking with other people's #defines is dangerous.

    Anyway...

    Code:
    #undef WHATEVER
    #define WHATEVER BlahBlah
    EDIT: Hmm. I misread your question.

    You need this in the header file:

    Code:
    extern int ThisNum;
    And in some module somewhere:

    Code:
    int ThisNum = 12;
    Then replace:

    Code:
    #define THISNUM ThisNum
    This is pretty awful.
    Last edited by brewbuck; 04-09-2009 at 09:56 AM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    Mucking with other people's #defines is dangerous.

    This is pretty awful.
    Yeah, it's not really intended as a permanent situation. Your solution works (extern int MyNum; #define MYNUM MyNum) in so far as I get a little further into the build and then get "undefined reference to MyNum".

    I did add "MyNum=12" somewhere, but that should not matter w/r/t to the fact that MyNum will have some value anyway (I actually moved MyNum=12 to the line before the undefined reference, same problem).

    Hack, yuck, hack, yuck, hack...
    Last edited by MK27; 04-09-2009 at 11:44 AM.
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The undefined reference is probably because you never actually defined MyNum. You need to put this in exactly one .c module:

    Code:
    int MyNum = ...;
    The extern declaration from the header file is not enough on its own to create the variable.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sweet! Brewbuck you're my new hero.

    Having never used extern before, I was using "Mynum =" instead of "int Mynum=" in the later definition.
    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. Trying to redefine library #defines
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2007, 07:30 AM
  2. redefine cout to directing one file, is impossible?
    By toysoldier in forum C++ Programming
    Replies: 8
    Last Post: 08-19-2004, 11:27 AM