Several uses of #pragma

Source: Internet
Author: User


Precompiled directives #pragma there are many ways to use it, and I'm sure many programmers will never encounter some of them in their lifetime,

, they are really remote and we are not using their needs. So, this article will not be exhaustive, just put several commonly used several summary

A bit.

1. #pragma once
The guarantee header file is only once, equivalent to
#ifndef _x_h
#define _x_h
...
#endif//_x_h

2. #pragma comment
The prototype is #pragma comment ("Comment-type" [, commentstring]), put comment recode into the target file or can

Execute the file. "Comment-type" has five kinds, which Lib is more commonly used: #pragma comment (lib, "d3d9x") in the connection will be

The D3d9x.lib newspaper is included in the project. In fact, the IDE can also manually set the Lib file to include in.

3. #pragma warning
You can do some processing on the compiled warning.
For example, when you turn float to int, there is a warning. See warning is always unhappy, and some warning heart know, certainly not

What will cause the problem when:
#pragma warning (disable:4244)
The warning can be shielded. #pragma the scope of the warning is this file and, if it is a header file, it affects the module that contains it, ensuring that

Your use will not affect others. So it's best to restore warning in time.
#pragma warning (push)
#pragma warning (disable:4705)
#pragma warning (disable:4706)
Some Code
#pragma warning (POP)
can also
#pragma warning (disable:4705)
#pragma warning (disable:4706)
Some Code
#pragma warning (default:4705)
#pragma warning (default:4705)


4. #pragma pack
C + + class,struct,union is aligned by default in Word, double word, four word boundaries. Sometimes we need to align our way,

For example, let the network bag more brow.
#pragma pack (push,1)
struct a{//Now sizeof (A) equals 5; By default it is 8;
int i;
char c;
};
#pragma pack (POP) is the same as warning when used, also pay attention to scope problems. I remember a guy who used to change his head in a file.

Qi way, not in time to set back. This bug is very annoying, will intermittently and inexplicably cause the program crashes, must be careful.

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.