Solved

doubt in the header file

Posted on 2010-08-20
16
620 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

776 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