Thread: Could you explain me #define works?

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    51

    Could you explain me #define works?

    I return to C programming after 12 years and forgot many things.

    In my C program I see:

    Code:
    #define ORA_CHECK_COMMIT(lda) \
      if(ocom(lda)) { \
        handle_error(EM_ORACLE, NULL, "ocom at", __LINE__); \
        exit(3); }
    But I cannot find the code, I want to know how it works. An idea?

    Thanks,

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What do you mean "find the code"?

    It's right there in front of you.

    If you see in the actual source something like
    ORA_CHECK_COMMIT(banana)

    The compiler will replace it with
    Code:
    if(ocom(banana)) {
        handle_error(EM_ORACLE, NULL, "ocom at", __LINE__);
        exit(3); }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    51
    I know it is preprocessor command, which will be replaced by
    Code:
    if(ocom(lda)) { \    handle_error(EM_ORACLE, NULL, "ocom at", __LINE__); \
        exit(3); }
    But I cannot find anywhere
    Code:
    ocom(lda)
    and handle_error
    Are they function? Where they can be found, in another program, or header file or somewhere else?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, both ocom() and handle_error() are functions or further macros.
    Both will almost certainly be in a header file - unless your code is so old it's full of implicit function declarations.
    Both functions will have to be in a library somewhere, otherwise your code will not build.

    Which compiler are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    51
    The code is more than 14 years old. It is AIX, compiler cc

  6. #6
    Registered User
    Join Date
    Apr 2017
    Posts
    51
    ocom() and others not in header. there is should be a library somewhere.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well if this were gcc, I would suggest

    gcc -E prog.c
    to find out what ocom() and handle_error() actually become after the pre-processor has done all it's work.

    To find the library, the handy linker option --trace-symbol would be great.
    ld(1): GNU linker - Linux man page

    Do you have even a basic tool like nm (nm(1): symbols from object files - Linux man page) to show you all the symbols in an object file / library file?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2017
    Posts
    51
    I put below just several lines example on Solaris
    Code:
    nm -AgrR *.o
    
    fin2ici.o: [37] |         0|         0|NOTY |GLOB |0    |UNDEF  |fin2ici.o:__iob
    fin2ici.o: [84] |         4|       268|OBJT |GLOB |0    |4      |fin2ici.o:$XAbOEmKTeX_WEsq.oci_func_tab
    fin2ici.o: [23] |      4055|        12|OBJT |GLOB |0    |5      |fin2ici.o:$XBbOEmKTeX_WEsq.d2i_CloseMq.__func__
    fin2ici.o: [54] |      3962|        16|OBJT |GLOB |0    |5      |fin2ici.o:$XBbOEmKTeX_WEsq.d2i_CloseOracle.__func__
    fin2ici.o: [46] |      3996|        17|OBJT |GLOB |0    |5      |fin2ici.o:$XBbOEmKTeX_WEsq.d2i_LeftPadField.__func__
    fin2ici.o: [44] |      4013|        15|OBJT |GLOB |0    |5      |fin2ici.o:$XBbOEmKTeX_WEsq.d2i_OpenCursor.__func__
    Could you please explain briefly, what it means?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try the manual page for 'nm' on your system.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2017
    Posts
    51
    I was looking for nm options, which shows me the path to our function/libraries. man nm says it is -A. It mentioned function names, but no path. Does it mean that they are also macro? And how can I understand it?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Macro definitions won't appear in an nm list.

    All I can suggest is create some simple source files (say a main + one function), then use nm to examine the .o file, until you can grok the output of nm sufficiently well to understand it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Apr 2017
    Posts
    51
    Sorry, but I have 3 days to analyze the code, which was written 15 years ago. I cannot play. I have to work.

    Thanks

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Suggest your employer finds someone competent at C as of now, not someone with a decade of rust to knock away.

    If the code has managed for 15 years, why the sudden panic to do something in 3 days?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-14-2016, 07:56 PM
  2. Replies: 5
    Last Post: 02-27-2014, 08:09 AM
  3. Can someone explain how Python code parser works?
    By 33volx in forum C Programming
    Replies: 5
    Last Post: 02-27-2014, 08:09 AM
  4. Pls explain how this program works...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-05-2002, 09:53 AM
  5. Can someone explain how this function works?
    By Drew in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2001, 01:09 PM

Tags for this Thread