Thread: Macro definition

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    69

    Macro definition

    Now getting this error when compiling this macro:


    Undefined symbols for architecture x86_64:
    "_main", referenced from:
    implicit entry/start for main executable
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)


    code:

    Code:
    
    
    Code:
    #include <stdio.h>
    
    
    #define swap(t,x,y) \
    
    
    ( t type = x; x = y; y = type; )
    
    
    
    
    
    
    int main()
    	
    {
    
    
    
    
    int x, y;
    x = 2, y = 10;
    
    
    swap(int, x, y);
    
    
    printf("%d %d\n", x, y);
    
    
    return 0;
    
    
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    If you have a function-style macro spanning multiple lines like this, enclose it within a do while(0) loop, e.g.,
    Code:
    #define swap(type, x, y) do {\
        type t = x;\
        x = y;\
        y = t;\
    } while (0)
    I have taken the liberty of um, swapping t and type.

    You really need to indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    69
    If you have a function-style macro spanning multiple lines like this, enclose it within a do while(0) loop
    I think that's matter of style. Also, I would prefer variables begin with underscore so that they won't clash with function or variable definitions in header files.

    Code:
    #include <stdio.h>
    
    
    #define SWAP(t,x,y) \
    { \
     t_type = __x; __x = _y; _y = _type; \
    }
    
    
    
    
    
    
    int main()
    	
    {
    
    
    
    
    int x, y;
    x = 2, y = 10;
    
    
    SWAP(int, x, y);
    
    
    printf("%d %d\n", x, y);
    
    
    return 0;
    
    
    
    
    }

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by coder222 View Post
    I think that's matter of style. Also, I would prefer variables begin with underscore so that they won't clash with function or variable definitions in header files.
    1. The do...while(0) macro trick is not a matter of style, it's to allow the macro to be used in places where a statement normally would be.
    2. The underscore trick is only needed for variables you create inside your function-like macro:

    Code:
    #include <stdio.h>
    
    #define swap(type, x, y) do {\
        type _t = x; /* Naming this t will cause problems */\
                     /* in the below code. */\
        x = y;\
        y = _t;\
    } while(0)
    
    int main(void)
    {
        int x = 100;
        int t = 200;
        int y = 300;
    
        printf("Swap x/t or t/y? [1/2]: ");
        char resp = getchar();
    
        if (resp == '1') swap(int,x,t); /* This will fail unless swap is
                                           wrapped with do {...} while(0) */
        else swap(int,t,y);
    
        printf("%d %d %d\n", x, t, y);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix product using macro definition
    By ElNoob in forum C Programming
    Replies: 5
    Last Post: 12-08-2012, 09:44 PM
  2. macro
    By siperi in forum C Programming
    Replies: 1
    Last Post: 11-15-2010, 04:32 AM
  3. Multiple definition error, one definition
    By frog in forum C++ Programming
    Replies: 9
    Last Post: 10-21-2010, 03:15 AM
  4. what is wrong with the macro definition....
    By sanddune008 in forum C Programming
    Replies: 7
    Last Post: 04-28-2010, 01:26 AM
  5. Macro definition
    By aayu09 in forum C Programming
    Replies: 11
    Last Post: 04-12-2010, 02:33 PM