Search code examples
c++cvisual-studiodll

confuse on building and using dll with c++


Background

I'm a rookie and I'm learning how to build and use dll in c++ through microsoft official guidance. https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170

I found that it use a header file with checking whether there exist certain macro(MATHLIBRARY_EXPORTS) to obtain two edition of MATHLIBRARY_API macro definition. Then use this header file either in DLL building project and client project.

I don't know where the macro(MATHLIBRARY_EXPORTS) definition is and I want to try building a DLL just use "__declspec(dllexport)", so I create two project to in visual studio to test.

Question

In the dll-building project, I only use "dllexport" before function declaration in header file and build a dll successfully. What confuse me is it can works while I include this header in client project and give me a correct result(print "hello world" in console). Is this normal? Why I don't need declare the function as "dllimport" and it can works?header fileclient projectresult


Solution

  • From the doc: Import into an application using __declspec(dllimport)

    Using __declspec(dllimport) is optional on function declarations, but the compiler produces more efficient code if you use this keyword. However, you must use __declspec(dllimport) for the importing executable to access the DLL's public data symbols and objects. Note that the users of your DLL still need to link with an import library.