Utility of #pragma

7

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 has pragma been made?

         

    What is your role in language?

         

    With the pragma comment , can credentials be entered into the system?

        
    asked by anonymous 28.09.2016 / 21:58

    3 answers

    6

    I'll start talking about this in In C / C ++, what are the 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 . p>

    Padding

    I specifically speak of pragma pack in Read objects saved in .dat file , How" padding "of C ++? and Why is the size of a struct not the sum of the sizes of your variables? .

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

    Include without repeating

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

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

    Comments for the linker

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

    #pragma comment(lib, "nomeDaBiblioteca")
    

    This indicates that the library is required to function.

    It could be a linking option that the compiler will do. Or even make some kind of signature in the code, something like:

    #pragma comment(user, "texto aqui")
    
        
    28.09.2016 / 22:13
    4

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

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

    For example:

  • there is an include file "a.h", with the pragma "once" in the first line
  • there is an include file "b.h" which includes "a.h"
  • there is an include file "c.h" which includes "a.h"
  • 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 "pc") includes "ch" and discovers that it should include "ah", but as "ah" has already been included in "bh", no new "ah" inclusion is made. p>

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

    struct
    {
       char c;
       int  i;
    } s;
    

    has a 3-byte "hole" between "c" and "i", to "align" the "i" address to a multiple address of 4, since it is usually more efficient for the processor to access an "int" which 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 accessing 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 "comment" pragma if I am not mistakenly embeds strings in the executable code, but I'm not sure.

        
    28.09.2016 / 22:13
    0

    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-default directive, indicates that the file where it resides will be ignored if added multiple times. So, once #pragma , has the same purpose as #include (guard), but with several advantages, including:

    • less codes
    • avoid name conflicts
    • increase in compilation speed.

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

    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:

    29.09.2016 / 14:47