[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

doubt in the header file

Posted on 2010-08-20
16
Medium Priority
?
628 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
14 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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 40

Expert Comment

by:evilrix
ID: 33483453
0
 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

872 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