What is dllspec(dllimport) and dllspec(dllexport) means

半世苍凉 提交于 2020-01-01 00:49:08

问题


After googling, i came to know that Dllimport makes the function available for other modules,

is it mandatory to declare function with extern "c" identifier?

Also, Dllexport means, Dll itself uses the function while compiling it says. so by default all

functions present in DLL are dllexport?


回答1:


__declspec(dllexport) exports a symbol. It makes it available from outside a DLL.

__declspec(dllimport) imports a symbol. It practically says "this symbol is not defined in this application, it needs to be imported from a DLL file".

You don't have to declare it with extern "C". If you don't use extern "C", then the symbol will be exported as a C++ symbol, and you will only be able to call it from C++ (and languages that support calling C++ DLLs). If you use extern "C", then the symbol will be exported as a C symbol, and you will be able to call it from languages that support caling C DLLs.

If you want to use your DLL in C#, you will need to use extern "C".

Here is an excellent tutorial that shows you how to use a C++ DLL in C#: How to marshal a C++ class. I have used solution A in many projects at work.

Also, here is a short tutorial on how you can use a C++ DLL in another C++ application: How to create and use DLL in C++.




回答2:


No -- dllexport means you're exporting it from the DLL (or from an executable) so that other modules (DLLs or executables) can use that function.

dllimport is used to declare a function that's implemented in a DLL (or, again, executable).

So, in a typical case, you'll have something like:

#ifdef BUILDDLL
#define DLL declspec(dllexport)
#else
#define DLL declspec(dllimport)
#endif

Then each public function the DLL will be marked as DLL:

DLL int dosomething(int);

Then, when you're building the DLL, you'll define BUILDDLL, to have all those functions marked as dllexport. Otherwise, you'll include the same header in client code that needs to use the function(s). It won't define BUILDDLL, so they'll all be marked as dllimport instead, so when it comes to link time, it'll create a link to that DLL instead of trying to satisfy those functions from someplace like the standard library.




回答3:


It also means that entries (in the form of static import and export tables) are created (by the linker) in the exe, dll..files, which document the dependencies between a provider and a consumer.



来源:https://stackoverflow.com/questions/10222566/what-is-dllspecdllimport-and-dllspecdllexport-means

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!