6

Possible Duplicate:
Windows & C++: extern & __declspec(dllimport)
Why/when is __declspec( dllimport ) not needed?

I want to write a DLL project. This project include several DLLs. They are dependent. I define some macros like follow :

#ifdef  MYDLL_DECL1
    #define  DLL_DECL __declspec(dllexport)
#else
    #define DLL_DECL __declspec(dllimport)
#endif

I defined MYDLL_DECL1...MYDLL_DECLn for each modules. Because I thought if i define the same macro that it wouldn't work . But I really want to define only one macro, and i wrote a testbed. I have two modules. In the second moudle's source file. I write code like follow:

#define  MYDLL_DECL
#include "moudle1.h"
#include "moudle2.h"

If I use the same macro name "MYDLL_DECL" ,for modle1's head file I have defined "MYDLL_DECL", so "DLL_DECL" is equal to '__declspec(dllexport)'. Actually in module2 it should be equal to "__declspec(dllimport)", Because module2 import module1. But I found it worked when I just define a same macro for two module. And I also find that the OpenCV also use this methold to its library

Community
  • 1
  • 1
Samuel
  • 5,977
  • 14
  • 55
  • 77
  • 2
    ... What?? You can use `__declspec(dllimport)` directly without needing to define your own macro! – Alvin Wong Jul 23 '12 at 06:25
  • 4
    http://stackoverflow.com/questions/4489441/why-when-is-declspec-dllimport-not-needed – Mohammad Jul 23 '12 at 06:31
  • __declspec(dllimport) is a specifier of C/C++. So I can use – Samuel Jul 23 '12 at 06:33
  • Are you asking about macros, or about `__declspec(dllimport)`? If you are asking about both then you should really put those two questions as separate SO questions. – Some programmer dude Jul 23 '12 at 06:47
  • Actually i want to ask about the two question. I have specified them . Sorry for my pool expression – Samuel Jul 23 '12 at 07:02
  • Hi Joachim Pileborg, can you help me to reopen this question. I have modified my question. Now it's not same as http://stackoverflow.com/questions/2288293/windows-c-extern-declspecdllimport and http://stackoverflow.com/questions/4489441/why-when-is-declspec-dllimport-not-needed – Samuel Jul 24 '12 at 03:30

1 Answers1

7

First, think about what you need without the macro. If a class or function is defined in module1, you need to declare it __declspec(dllexport) in module1, and __declspec(dllimport) in all of the other modules. Including in the header file where it is declared.

Since you don't want to maintain two different header files, and you don't what conditional compilation all over the place, the best solution is use a conditionally defined macro, e.g.:

#ifdef MODULE1
#define MODULE1_DECL __declspec(dllexport)
#else
#define MODULE1_DECL __declspec(dllimport)
#endif

When invoking the compiler, you only define MODULE1 in the project module1; you don't define it in any other project. So when compiling module1, MODULE1_DECL expands to __declspec(dllexport), and when compiling any other module, it expands to __declspec(dllimport).

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • It also need to define MODULE1...MODULEn. I only want to define only one macro. I define one macro for all the modules. And it works.because I found that when I want to import other dlls. I dont't need the specifier __declspec(dllimport) . – Samuel Jul 23 '12 at 08:15
  • Hi James Kanze, can you help me to reopen this question. I have modified my question. Now it's not same as http://stackoverflow.com/questions/2288293/windows-c-extern-declspecdllimport and http://stackoverflow.com/questions/4489441/why-when-is-declspec-dllimport-not-needed – Samuel Jul 24 '12 at 03:29
  • @Samuel I don't know how this forum works, except that things arbitrarily happen, for some irrational reasons. All I can say is that if you don't use the specifier `__declspec(dllimport)`, and the symbols are imported from another translation unit, you have undefined behavior. It might work, for some types of symbols, but you can't count on it. – James Kanze Jul 29 '12 at 15:23