Solved

doubt in the header file

Posted on 2010-08-20
16
605 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 8

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 8

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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now