Thread: function version info

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    8

    function version info

    I'm looking for a way to be able to identify which functions and header files are included in an exe file.

    I've been using: char function1_version[] = "function1 0.1.1";

    but this does not work if I insert something like it in a header file that is shared by a number of files.

    Appreciate any suggestions.

    Thanks,
    RON C

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That depends, which compiler are you using, and how much are you willing to sacrifice portability?

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    I'm using VC++ .net (2005 and 2003). Therefore portability is not a worry.

    Thanks,
    RON C

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could use a static const char *:
    Code:
    #include <stdio.h>
    
    #define FUNCTION_VERSION "1.1.1"
    
    void function(void) {
        static const char *version = __func__ "() version " FUNCTION_VERSION;
    
        puts(version);
    }
    
    int main(void) {
        function();
    
        return 0;
    }
    Output:
    Code:
    function() version 1.1.1
    (__func__ is a C99 macro: a string containing the name of the current function. It's infinitely useful for debugging. Other standard C89 macros are __FILE__, __DATE__, __TIME__, and __LINE__.)

    The above program takes advantage of the fact that string will be concatenated together by the preprocessor:
    Code:
    "Hello, "
    "World!\n"
    ->
    Code:
    "Hello, World!\n"
    Last edited by dwks; 01-31-2006 at 05:33 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    dwks,
    Thanks a bunch. I'll have to try it.

    Any idea how to do it with header files that are included in multiple cpp files? The link step gripes about multiple instances.

    Thanks,
    RON C

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by rechmbrs
    The link step gripes about multiple instances.
    You're not putting source code in a header, are you?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    No. I use the header files for global variables etc. They are changed every once in a while so I'd like to be able to id which one is included.

    There are a number of people working on common code.

    In the functions, I have something like this: extern char doframes_version[] = "doframes_v001"; where doframes is the function name. Can I use __FILE__ in that to replace the two instances of doframes? This works very well but I had to add that code for over 150 functions (naturally replacing the name).

    Thanks for your help.
    RON C

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The header has something like this, right?
    Code:
    extern char doframes_version[];
    And only the source code has something like this, right?
    Code:
    char doframes_version[] = "doframes_v001";
    (Although preferably const.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    At present, I have nothing in the header files as this gives the duplicate error stuff because the header file is included into many function files.

    I do have the extern on the source code version. Not needed?

    Thanks,
    RON C

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A hideous example:
    Code:
    /* foo.h  */
    #ifndef FOO_H
    #define FOO_H
    
    extern const char foo_version[];
    void foo(void);
    
    #endif
    Code:
    /* foo.c  */
    #include "foo.h"
    
    const char foo_version[] = "foo v1.0";
    void foo(void)
    {
    }
    Code:
    /* bar.h */
    #ifndef BAR_H
    #define BAR_H
    
    extern const char bar_version[];
    void bar(void);
    
    #endif
    Code:
    /* bar.c  */
    #include "bar.h"
    
    const char bar_version[] = "bar v2.2";
    void bar(void)
    {
    }
    Code:
    /* baz.h  */
    #ifndef BAZ_H
    #define BAZ_H
    
    extern const char baz_version[];
    void baz(void);
    
    #endif
    Code:
    /* baz.c  */
    #include "baz.h"
    
    const char baz_version[] = "baz v2.0";
    void baz(void)
    {
    }
    Code:
    /* main.c */
    #include <stdio.h>
    #include "foo.h"
    #include "bar.h"
    #include "baz.h"
    
    int main ()
    {
       puts(foo_version);
       puts(bar_version);
       puts(baz_version);
       getchar();
       return 0;
    }
    
    /* my output
    foo v1.0
    bar v2.2
    baz v2.0
    */
    Last edited by Dave_Sinkula; 01-31-2006 at 10:52 PM. Reason: bar.c and bar.h twice! We know I've never passed a bar yet!!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    Still have a problem. My #include file boyj contain the same items where one is defined and the other is set to extern ( int aaa =5; & extern int aaa; ).

    How do I handle that?

    Thanks,
    RON C

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Put the extern declaration in the header. Add the definition in one source file.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    8
    I have two header files with one having and one not having the extern. I'd prefer not to move the declaration into the main routine if possible.

    Thanks,
    RON C

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    #include one header in the other?
    Use macro trickery in the header?
    Provide its definition in its associated module rather than the main?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    The best way that I know to do what you're trying to do is to create a variable in each header file of the form
    Code:
    int VSTRING_headername_version;
    and then look at the symbol table for the entries that begin "VSTRING_". Note that you never initialize this variable, and it's not declared extern. The linker should combine all occurrences of one of these into a single storage location. In years past, I've seen this done, but I don't know if it works with VC++ of any flavor. One of the downsides is that you can't get the info from an executable that does not have a symbol table.

    I don't recall if the above technique violates anything in the C90 standard, but because the behavior that it depends on was used by so many pre-ANSI C programs, most compilers/linkers did support it (even though it is probably depricated usage).

    Some compilers create a symbol in the object file symbol table for each file encountered during compilation, and those symbols may also be stored in the linked images symbol table.

    None of these does exactly what you want, however.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM