# dll import and export

This topic is 4047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

There's something I don't understand with __declspec(dllimport) and __declspec(dllexport). Let's say I've a DLL named Utils.dll utils.h
extern "C" __declspec(dllexport) int SomeFunction ();


utils.cpp
int SomeFunction ()
{
return 1;
}


then I've another DLL named DLLThatUseUtils.dll DLLThatUseUtils.h
extern "C" __declspec(dllimport) int SomeFunction ();


Now, inside DLLThatUseUtils I should be able to call SomeFunction() beacause I've imported but: 1) If i don't link Utils.dll with DLLThatUseUtils.dll I get the "unresolved external symbol" 2) If i link...well if I link which is the purpose of __dllimport ? If I staticaly link the 2 dlls, then there's no need to __dllimport SomeFunction, isnt'it ? Thank you

##### Share on other sites
If you want to load the dll during runtime, for a Plugin system for example you must use LoadLibrary and GetProcAddress.

You need to tell the linker that the function is not in one of DLLThatUseUtils object files, and you need to tell it in which dll it is, thus the need of __declspec(dllimport) and the need to link against the dll.

If you are bothered with the need to use __declspec and you're not doing something that requires runtime dll loading, you can use a .def file here and here.

##### Share on other sites
Thank you for reply but I still don't get it.

I'm talking about the use/mean of __desclspec(dllimport).

If I staticaly link a DLL with another DLL or with and exe, there is no need to dllimport any functions.
All I need is __dllexport (or to use a .def file), include the right .h and the compiler will work just fine.
So I still don't understand the use of __declspec(dllimport)

##### Share on other sites
Quote:
 Original post by GiallanonIf I staticaly link a DLL with another DLL or with and exe, there is no need to dllimport any functions.All I need is __dllexport (or to use a .def file), include the right .h and the compiler will work just fine.So I still don't understand the use of __declspec(dllimport)

I think you're either misunderstanding something here or using the wrong terminology, you cannot by definition "statically link to a dll", you can link to a static library (.lib) or link to a dynamic library (.dll), so "statically linking to a dll" is an oxymoron. The .lib file you link to when linking to a dll does not contain any object code and is just a helper to get to the dll.

I don't know exactly what are you doing here, as far as I understand it, you do need __declspec(dllimport) to let the linker know that it should look for the function or resource address during the program loading stage, rather than hardcode it to a known piece of object code even if linking against the "helper" lib file rather than the static one.

In short, __declspec(dllimport) is needed to let the dynamic linker know that it should look for the resource in an external PE file (exe/dll), if you link to a static library, the object code for the function is internally copied into the executable, so no need for __declspec(dllimport).

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
11
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631777
• Total Posts
3002305
×