Solved

doubt in the header file

Posted on 2010-08-20
16
621 Views
Last Modified: 2013-12-14
What is the use of defining UTILS_DEF? And why is its name used in front of each class?

#if !defined(UTILS_DEF)
#   ifdef  WIN32
#       ifndef IPA_UTILS_EXPORTS
#           define UTILS_DEF __declspec(dllimport)
#       else
#           define UTILS_DEF __declspec(dllexport)
#       endif
#   else
#       define UTILS_DEF
#   endif
#endif  // !defined(UTILS_DEF)

#include "AException.h"

namespace IPA_MibDefinition
{    
  class UTILS_DEF SystemRelease
    {
    public:
 
        SystemRelease( const std::string & );
 
        bool operator< ( const SystemRelease & );
        bool operator==( const SystemRelease & );
        bool operator> ( const SystemRelease & );
 
    private:
 
        std::string m_value;
    };
 }
0
Comment
Question by:pvinodp
  • 4
  • 3
  • 3
  • +2
16 Comments
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 33483404
In place of that You can use any meaningful name. Here it is a header  guard.

If u are including same header more than once in a .cpp/.c file, you won't face any problem due to this guard. This willn't allow to include a header file more than once.

For more info can follow:
 http://www.codeproject.com/KB/cpp/cpp_header_guard.aspx

0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 33483411
In place of that You can use any meaningful name. Here it is a header guard.

If u are including same header more than once in a .cpp/.c  file, you won't face any problem due to this guard. This willn't allow to include a header file more than once in a .cpp file.

For more info can follow:
 http://www.codeproject.com/KB/cpp/cpp_header_guard.aspx
0
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 33483446
This is nothing to do with header guards.

If UTILS_DEF is not defined then the compiler (if building on a WIN32 platform) will  try to either export or import various functions to support DLL semantics. The definition of this prevents the compiler applying DLL semantics so that the library can be built as  a static library instead.

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 40

Expert Comment

by:evilrix
ID: 33483453
0
 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 250 total points
ID: 33483464
UTILS_DEF is a macro that is __declspec(dllexport) if you compile this file as part of a library project (DLL) where another macro IPA_UTILS_EXPORTS defined.
It is  __declspec(dllimport) if this file is included into  an executable that uses this library.

MSDN: http://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx
__declspec(dllexport) allows to export functions from dll
__declspec(dllimport) allows to import
0
 

Author Comment

by:pvinodp
ID: 33486138
Assuming that Win32 is defined:
so according to the condition the class which are prefixed by the key word UTILS_DEF can be a dll which is exported or imported

Am i correct?

So what is the purpose of  UTILS_DEF when Win32 is not defined?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33486171
Empty. It allows to avoid the compilation error about the undefined symbol.
 
0
 

Author Comment

by:pvinodp
ID: 33486518
pgnatyuk:
I dint understand ur explanation.. could u be more detail?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33486558
If UTILS_DEF  is left undefined the compiler will generate a compile time error because it will be used in declarations.

Look at...

class UTILS_DEF SystemRelease {}

if UTILS_DEF was left undefined this would be seen as syntactically erroneous by the compiler. If defined as nothing the precompiler converts this to

class SystemRelease {}

and this is not erroneous.

0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33486605
Detail about what?
That's what it is:
#define UTILS_DEF

You use #define when you need to give a name for numeric or text constant. An example:
#define PI 3.1415
So in the code you can write:
float S = PI * r * r;
Compiler will replace PI with 3.1415

In our case UTILS_DEF is empty.

BTW, who made this picture from the definitions? :) Looks funny. Kind of a new art. :)
0
 

Author Comment

by:pvinodp
ID: 33486627
got it..
Thanks
0
 
LVL 5

Expert Comment

by:shajithchandran
ID: 33488288
Looking at precompiled output can help you simplify this is I guess. Usually in command line , -E is the option to get the precompiled output. The precompiled out put will have all the macros processed, hence you can see the final output of how the file will look.
0
 
LVL 5

Expert Comment

by:shajithchandran
ID: 33488388
opps! in my above post, precompiled output should be read as preprocessed output.
0
 

Author Closing Comment

by:pvinodp
ID: 33509470
The "include here is a way to determine if the dll is import or export.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question