Several useful macros

Source: Internet
Author: User

1.Print error message

If the execution of a program requires that a macro be defined, you can use # error, # warning to print the error (warning) information when the macro is not defined. For example:

# Ifndef _ UNIX __
# Error "This section will only work on UNIX systems"
# Endif

The program can be compiled normally only when the _ UNIX _ macro is defined.

 

2.Easy debugging

_ File, _ line, and _ function are macros predefined by the compiler. They represent the file name, row number, and function name of the current Code respectively.

 

You can add the following statement to the Code to track code execution:

If (ERR ){

Printf ("% s (% d)-% s \ n" ,__ file __,__ line __,__ function __);

}

 

3.C/C ++Hybrid programming

Function int Foo (int A, int B );

In C language, the name of this function in the library after the compiler compilation is _ Foo, in C ++, after the function is compiled, its name in the library is _ foo_int_int (changes made to implement function overloading ). If C ++ needs to use the library function compiled by C, the system will prompt that the function cannot be found because the symbol name does not match. Use extern "C" in C ++ to solve this problem. It indicates that the function to be referenced is compiled by C and should be searched for Symbols according to the naming method of C.

 

If foo is a C-compiled library and you want to use Foo in C ++, add the following statement, where _ cplusplus is a predefined macro of the C ++ compiler, this file is compiled by the C ++ compiler. If you reference the C library function, you need to add the extern "C ".

 

# Ifdef _ cplusplus

Extern "C "{

# Endif

Extern int Foo (int A, int B );

 

# Ifdef _ cplusplus

}

# Endif

 

4.Variable parameters

# Define debug (format, argS...) fprintf (stderr, format, argS)

# Define debug (format,...) fprintf (stderr, format, _ va_args __)

Or # define debug (format,...) fprintf (stderr, format, ##_ _ va_args __)

The first two have redundant commas. The third macro uses the # Remove unnecessary commas.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.