Posts tagged #pragma
Easy Compilation guards instead of #ifndef macros
0![]()
Did you ever noticed the following lines in the your header file?
#ifndef _MYHEADER_H_ #define _MYHEADER_H_ ... #endif
They are protection macros to protect the header file from multiple compilation. Assume your header file is included many time in different CPP. if you are not using these protection macros, your header file will be included more than once and will give you a Multiple declaration error. To prevent that, these macros are being used. But there is a more simple method in VC++ compiler extension.
![]()
You can use #pragma once in your header file. E.g.
#pragma once ...
If you use #pragma once, then the header file will be included only once in your compilation.
Embed a copyright notice in EXE
0![]()
You can embed any strings in your exe or dlls.
1) It can be used for copyright messages etc…
2) It can be used in Export DLL header files to know which version of DLL is linked in the exe.
Apply your imagination for more usages…
![]()
Use the #pragma directive. See the usage below.
#pragma comment(exestr, "This is my Exe. Don't copy it away!");
#pragma comment(exestr, "GraphicLibrary Version 1.01.");
![]()
Open the exe in textpad for seeing the data. Opening in notepad will not work.
Automatically link required libraries.
0In our projects we use number of 3ed party API’s especially from platform SDK and other components and one burden is finding the libraries for those API’s. For Microsoft provided API’s since they have strong documentation, finding the library for particular API is not so tedious. But for other vendors, It may be. We need to dig a lot for finding the lib of that particular API. How can we get rid from this?
You can you #pragma comment( lib, “LibraryName” ) to specify the necessary library file in the header file itself. For e.g. See the following header file.
// EventLog.h class EventLog { … }; #ifdef _DEBUG // If it’s a debug version… #pragma comment( lib, "MyEventLogLibd.lib" ) #else // Ooooh!!! Its release version. #pragma comment( lib, "MyEventLogLib.lib" ) #endifIf we include the EventLog.h header file, the library required will be automatically included. I dream for the day on which all SDK headers auto includes their libraries…
Target audiance – Intermediate.