The role of #pragma message

Source: Internet
Author: User

In general, #pragma message (messagestring) is a text string (messagestring) sent to the standard Output window during compilation. A typical use method is to report and display information at compile time. The following code snippet is a message that is displayed in the standard Output window during compilation:


#if _m_ix86 = = 500
#pragma message ("Pentium processor Build")
#endif

Messagestring Ginsengnumber can extend a literal string constant to a macro that can display any form of characterString. For example, the following statement shows the name of the compiled file and the date and time when the file was last modified:


#pragma message ("compiling" __file__)
#pragma message ("Last modified on" __timestamp__)
#pragma message ("Remember to write some actual code in this sample app!!")

Let's use this feature to further extend the use of #pragma message. The following macro is defined first in a header file (assumed to be lfpragma.h):

#define PTODO_LINENUMBER_TO_STRING (x) #x
#define PTODO_LINENUMBER (x) ptodo_linenumber_to_string (x)
#define Include_file_and_line (String) __file__ "(" Ptodo_linenumber (__line__) "):" String

Then include the Lfpragma.h file in any CPP file, then we can refer to the previously defined macro as follows. Such as:

#pragma message (Include_file_and_line ("compiling") __file__)
#pragma message (Include_file_and_line ("Last Modified on") __timestamp__)
#pragma message (Include_file_and_line ("Remember to write some actual code in this sample app!!"))

What is the difference between using #pragma message and the usual method? Normally when we use #pragma message, we can see the output information of #pragma message in the standard Output window.However, the "Go to Error/tag" menu item in the context menu of the right mouse click popup is grayed out, not available. However, if you use the macro defined above, we can activate the "Go to Error/tag" in the context menu to navigate to the corresponding line of code

Pragma message is used to tell the programmer that the program is at compile time. And OUTPUTDEBUGSTR is the information that tells the programmer that the program is in the run time.

Here is an example of the pragma message.

cooperate with #if/#ifdef/#ifndefSet the prompt information at compile time, e.g.:

Test.cpp
#define UNICODE
#ifdef UNICODE
#pragma message ("Using the Unicode character set")
#else
#pragma message ("Use ANSI character set")
#endif
int main ()
{
return 0;
}


The role of #pragma message

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.