0

Is there any difference between class __declspec(dllexport) class_name and __declspec(dllexport) class class_name?

I know it seems odd. But a strange thing happened to my code. First I open my c++ Dll project in Visual Studio 2017 and add a class as usual. Compiling process is fine. But when I use another console to use my Dll, the linking process told me that unresolved external symbol referenced. What confused me most is that my new class's constructor and destructor functions are those missing symobls. Here I use the implicitly-declared default constructor and destructor.

I tried to explicit the constructor and destructor, It doesn't work and it still told me that I missed these two function.

I tried to check the calling convention. The dll and console are all set to __cdecl and I believe that's not the reason.

I suppose there is something wrong with my "#include". I tried, but nothing good happend.

I accidently exchange the order of the name of the class and right result came out.

Now, I'm really confused about __declspec(dllexport). If there is no difference between these two, how to explain this?

Note that I define the PATHPLANNINGLIB_EXPORTS macro in the preprocessor definitions.

#ifdef PATHPLANNINGLIB_EXPORTS
#define PATHPLANNINGLIB_API __declspec(dllexport)
#else
#define PATHPLANNINGLIB_API __declspec(dllimport)
#endif

namespace CCSP_PlanningAlgorithm
{  
using PathMap = std::map<SERobot*, std::vector<WayPoint>>;  

//PATHPLANNINGLIB_API class MOutput  //sucess declartion  
class PATHPLANNINGLIB_API MOutput    //failed declartion, why??  
{  
public:  
  long path_result_ID;  
  PathMap path_map;  
};  
}//CCSP_PlanningAlgorithm  

I expected everything goes fine. I just use this class to save my final data result. But the linking process failed and I can't run my program.

The test console program gave me this error message:

LNK2019 无法解析的外部符号 "__declspec(dllimport) public: __cdecl CCSP_PlanningAlgorithm::MOutput::MOutput(void)" (__imp_??0MOutput@CCSP_PlanningAlgorithm@@QEAA@XZ),该符号在函数 "public: static void __cdecl MyTestCase::myStaticTest(void)" (?myStaticTest@MyTestCase@@SAXXZ) 
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • 2
    There is no difference, note that in your error message, you have a **dllimport**, not export, meaning that you are telling the linker to import the symbol from somewhere else. – Matthieu Brucher Dec 21 '18 at 19:27
  • @MatthieuBrucher Yes, you are right. I think that's the reason. But I wonder how to miss the "PATHPLANNINGLIB_EXPORTS" macro? I defined this macro in a seperate .h file and I include this almost everywhere. – user10821458 Dec 21 '18 at 19:40
  • You export when building the dll. You import when using the dll. Usually you use a macro that switches these two depending on a defined preprocessor definition that you define in the DLL project. In your example `PATHPLANNINGLIB_EXPORTS` should be defined as a preprocessor definition in the dll project and no where else. That will make `PATHPLANNINGLIB_API ` equal to `__declspec(dllexport)` when you are building the dll and `__declspec(dllimport)` when you use the dll in some other project. – drescherjm Dec 21 '18 at 19:43
  • This should be a compiler side definition, `-Dxxx_EXPORTS`, otherwise you can't properly import your library with this header. That's the usual and most sensible usage. – Matthieu Brucher Dec 21 '18 at 19:46
  • Might want to check https://stackoverflow.com/questions/30581837/linker-error-when-calling-a-c-function-from-c-code-in-different-vs2010-project/30583411#30583411. – CristiFati Jan 04 '19 at 12:45

0 Answers0