Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

doubt in the header file

Posted on 2010-08-20
16
Medium Priority
?
627 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

610 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