Advertisement

Pragmas

Started by August 10, 2000 09:53 AM
4 comments, last by Ataru 24 years, 4 months ago
Does anyone know what Pragmas are and what they''re used for? They creep up every now and again, but it''s hard to get any info on them.
They are compiler directives (ie they tell the compiler to do stuff ). You can use them to turn off struct padding or supress certain warnings for example.

Edited by - Muzzafarath on August 10, 2000 12:14:08 PM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Advertisement
#pragmas tell the preprocessor/compiler/linker to do something.
#pragma once 
tells the compiler to only include the include file once in the whole project. This is a wrapper for
#ifndef INCLUDE_FILE_NAME_H#define INCLUDE_FILE_NAME_H// include file...#endif 


You can also use pragmas to disable warnings and other useful stuff.

------------------------------
#pragma twice
Here''s another handy one:

#pragma comment(lib,"") 


That tells the compiler to add the specified library file to the project so that it will be linked. I''m not sure if it''s Microsoft specific or not though (since VC is the only compiler I use).
The most important thing about pragmas is that they won''t break the compile if you compile the file on a compiler that doesn''t support the same pragmas as another.

Meaning, if you are compiling for VC++, and you use

#pragma message("Compiling Debug mode")

or something, to notify you of something when you''re compiling, and then you go to compile it with the Borland compiler, and it doesn''t support #pragma message, there will not be a compiler error. The compiler will just ignore the #pragma.

Basically, it provides a way to use some compiler-specific features without breaking compatibility with other compilers.
i tend to shy away from pragmas (hell - i shy away from anything starting with a # - pity #include has one...bring on vc7 with include directives!)

if you are using a pragma to disable a warning, you should be suspicious of bad program design (unless it''s that annoying STL warning, 4786!!! - if you know what im talking about, you''ll understand!)

This topic is closed to new replies.

Advertisement