In a DLL, I would like to call a function defined outside the DLL. Alternatively, I can add the function to the DLL, but it would use variables defined outside the DLL. I've tried both approaches, but they generate the "unresolved external" linker error. The external symbols, either a function or variables, are defined in the main program using the DLL.
Is there a way to have, in a DLL, symbols that are external to the DLL?
A simple example of the problem could be as follows:
// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
void ExternalFunction();
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" __declspec(dllexport)
LRESULT CALLBACK DummyFunction()
{
ExternalFunction();
return 0;
}
EDIT: There is a related problem called "circular dependencies between dlls with Visual Studio." However, this question doesn't concern the "unresolved external" linker error; the OP had apparently solved this problem. There are ten answers, varying strongly. Four answers claim no solution, two show code in other languages, two suggest solutions outside Visual Studio, one contains a sole article link, and one presents a seemingly incomplete solution. None of the answers is marked as an accepted solution. I don't see how the answers may be applied to solve my problem, unless there is no solution at all. I think my question deserves an answer.
I've now implemented the proposal of Alan Birtles. The pointer to ExternalFunction()
is passed to the DLL using a new function exported from the DLL and then used in the DLL as required. It works fine. This would be a very good and unique answer.