Thread: need a macro that does nothing

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    210

    need a macro that does nothing

    I'm trying to write (and debug) a module for a library. All functions have to be statics so they don't show up as one of the library's symbols. Unfortunatly this also prevents their names from showing up in any backtraces.
    Now I'm either looking for a simple way to switch between a debug compile where those functions are non-static and a build where they are marked static - or a way to make statics show up in my backtraces (glibc:backtrace()). I figured the former should be simple......

    For other debug functions I used the usual ((void)0) macros, so the obvious thing was to try something like...

    Code:
    #ifdef DEBUG
      #define _static ((void)0)
      [...]
    #else
      #define _static static
      [...]
    #endif
    (I named the macro "_static" instead of "static" because I don't want this to affect statics-in-a-function)

    As you might have already guessed, ((void)0) doesn't seem to be working for me in this case. I'm getting tons of errors like

    Code:
    client.c:3023: error: Syntaxfehler before "void"
    client.c:3024: error: Syntaxfehler before '(' token
    So the question would be how to make a macro that does exactly nothing, or how would I tell gcc not to strip names for static symbols (the manual didn't help me, btw).
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  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
    Read the manual page for the linker, and look up the options
    -S
    -s
    --retain-symbols-file

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    It's simpler than you think.
    Code:
    #define nothing
    On a side note, symbols with a leading underscore are typically reserved for use by the compiler.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Ouch, thanks for directing me to the linker manual. I was searching the gcc manual...

    So, do I understand this right, you say I should remove all "static"s from the source and define only those symbols I want to retain instead? That sounds like a neat solution as there are only 2 functions that need to be accessible.

    I did that. nm -g (show external symbols) only lists the two symbols I listest in the symbols-retain-file, but nm -D (show dynamic symbols) now shows the ones I made non-static and I'm not sure if that is a problem or not. Is it now possible to link against those in the .so-file by accident?

    On a sidenote, I just realized I know next to nothing about linking. Google is my friend, but if anybody got some link (uhm, no pun...) to a really good document, I'd really like to know it
    Thanks for helping so far, Salem!

    edit: Thanks anonytmouse, I didn't see your post in time and will try that too, but Salem got me interested...
    edit2: Yup that really works.
    Last edited by Nyda; 11-18-2004 at 08:17 AM.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    #ifdef DEBUG
      #define _static //_static does nothing
      [...]
    #else
      #define _static static //_static is static
      [...]
    #endif

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Be careful, _static might introde on your compiler's namespace. According to the C standard, if you want to be sure to avoid such conflicts you should use a trailing underscore instead (e.g. static_).
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM