Usefulness of #pragma

Asked

Viewed 4,855 times

9

Several C/C++ codes have the word pragma. Depending on the way it is implemented it has a different function.

  • #pragma Once
  • #pragma pack()
  • #pragma comment(xx, "")
  • For what purpose the pragma was made?

    What is its function in language?

    With the pragma comment, it is possible to enter credentials in the system?

    3 answers

    8


    I start talking about it in In C/C++, what are build directives for? When should I use them?.

    All of these are used by the Microsoft compiler, so far as I know exclusively, therefore not portable.

    All can be seen in official documentation.

    Padding

    I’m talking specifically about pragma pack in Read objects saved in.dat file, How the C/C "padding" works++? and Why is the size of a struct not the sum of the sizes of its variables?.

    It serves to reduce the size of a structure and not make alignment. Useful in very rare situations.

    Include without repeating

    The pragma once used to prevent a include is placed again where it was already included once during that compilation unit. The most portable way to do this is by defining indicative variables that have already been included. Dei example of this in another question.

    Without it there would be waste in the compilation having to analyze what had already been done.

    Comments for the Linker

    The pragma comment is used to include a comment in the object code generated by the compiler and which may be read by Linker to do something specific. An example:

    #pragma comment(lib, "nomeDaBiblioteca")
    

    This indicates that the library is needed to work.

    Could be an option linkediting which the compiler will do. Or still make some kind of signature in the code, something like:

    #pragma comment(user, "texto aqui")
    

    I put in the Github for future reference.

    • how can I be making a signature?

    • I never did, I believe it would be something like this #pragma comment(user, "texto aqui").

    4

    Pragmas are "directives" for the compiler, which affect the compilation process.

    Pragma "Once" is usually (always ?) used at the beginning of a file . h that will be included in other source files . c or . cpp (or even other .h). This pragma instructs the compiler to read this file only once, even if the compilation flow of a source implies reading it more than once.

    For example:

    1. there is a include file "a. h", with pragma "Once" in the first line
    2. there is a include "b. h" file that includes "a. h"
    3. there is a include "c.h" file that includes "a. h"
    4. there is a "p.c" file that includes "b. h" and "c.h"

    The compiler, reading "p.c", includes (reads) "b. h", and reading "b. h" also includes "a. h". Then the compiler (still processing "p.c") includes "c.h" and discovers that it should include "a. h", but as "a. h" has already been included in "b. h", no new inclusion of "a. h".

    The pragma "pack" controls the alignment of members of a structure. Normally, for reasons of efficiency a structure like this:

    struct
    {
       char c;
       int  i;
    } s;
    

    has a 3-byte "hole" between "c" and "i", to "align" the address of "i" at a multiple address of 4, as it is usually more efficient for the processor to access an "int" that has a multiple address of 4.

    Using a pragma pack(1), for example, we can avoid the occurrence of these "holes" at the cost of a probable inefficiency in access to the "i" member of the structure.

    #pragma pack(1) // força alinhamento de 1
    struct
    {
       char c;
       int  i;
    } s;
    #pragma pack() // volta o alinhamento default
    

    The pragma "comment" if I’m not mistaken embeds strings in executable code, but I’m not sure.

    1

    Pragmas are directive, which is a construction of some programming languages that specifies how the compiler or assembler should process the source code.

    Used in programming languages (C and C++), #pragma once is a non-standard directive, indicating that the file where it resides will be ignored if included multiple times. So, once #pragma, has the same purpose as #include (guard), but with several advantages, including:

    • fewer codes
    • avoid name conflicts
    • increase in the compilation speed.

    This directive is most useful for programs that are exceptionally large or need to take advantage of the capabilities of a particular compiler.

    The pragma comment places a comment record in an object or executable file. The comment can be read by Linker when it processes object files.


    Reference:

    Browser other questions tagged

    You are not signed in. Login or sign up in order to post.