1

what I've got here is pretty much a dumb and noobie question, but I just can`t find an answer on the web. When I want to create a DLL project (out of an existing c++ project), I read that I need to implement the following lines in the beginning of the .h files:

#ifdef _EXPORTING
#define CLASS_DECLSPEC    __declspec(dllexport)
#else
#define CLASS_DECLSPEC    __declspec(dllimport)
#endif

I looked at the example in the MSDN:

// MathFuncsDll.h
#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport) 
#else
#define MATHFUNCSDLL_API __declspec(dllimport) 
#endif

Now I want to understand, do I need to change the "_EXPORTING" and the "CLASS_DECLSPEC" for every new class I make? For example if I'd create a class named "foo" within the same project as "MathFuncsDll.h" I'd need to put the following lines at the start of the .h file:

// FooDll.h
#ifdef FOO_EXPORTS
#define FOO_API __declspec(dllexport) 
#else
#define FOO_API __declspec(dllimport) 
#endif

Or is some line is the same in all .h files of the project?

And another thing, if I use a namespace to reference the whole dll as one and extract the classes from it, do I need to put the using namespace *NAME* in every .h file?

Yechiam Weiss
  • 261
  • 4
  • 12

1 Answers1

2

No, you need not to create new macros for each classes:

class MATHFUNCSDLL_API Foo {...};
class MATHFUNCSDLL_API Boo {...};
class MATHFUNCSDLL_API MyNewClass {...};

For the second q: don't use using namespace inside header file: "using namespace" in c++ headers

Your header can looks like the following:

#pragma once
namespace foo {
    class MATHFUNCSDLL_API Foo {...};
    class MATHFUNCSDLL_API Boo {...};
    class MATHFUNCSDLL_API MyNewClass {...};
}

EDITED

// mylibdef.h
#pragma once
#ifdef _EXPORTING
#define CLASS_DECLSPEC    __declspec(dllexport)
#else
#define CLASS_DECLSPEC    __declspec(dllimport)
#endif

// myclass1.h
#pragma once
#include "mylibdef.h"
namespace mylib {
class CLASS_DECLSPEC MyClass1 {...};   
}

// myclass2.h
#pragma once
#include "mylibdef.h"
namespace mylib {
class CLASS_DECLSPEC MyClass2 {...};   
}
Community
  • 1
  • 1
AnatolyS
  • 4,249
  • 18
  • 28
  • @YechiamWeiss: no, I told you use one macro, which you can put into separate header mydlldef.h and include one into your headers. Also you can create separate header for each class and use namespace and marco as I show. – AnatolyS May 18 '16 at 09:16