views:

123

answers:

2

I have to interdependent dll here that i would like to build without having to build them twice (force build both of them and rebuild them again to allow linking).

Here is an exemple :

**DLL A**

void fooA()
{
  fooBB();
}
void fooAA()
{
  fooB();
}

**DLL B**

void fooB()
{
  fooA();
}

void fooBB()
{
}

Is there a way to build those two DLL without mayor refactoring?

+1  A: 

You could use LoadLibary and GetProcAddr to find functions' addresses and then call foo-functions by its addresses.

The following code demonstates how to do it (you should add error checking in real DLLs):

**DLL A**

typedef void fooB(void);
typedef void fooBB(void);
fooB* fooB_ptr;
fooBB* fooBB_ptr;
HMODULE hDllB;

void init()
{
  hDllB = LoadLibrary(L"DllB.dll");
  fooB_ptr = reinterpret_cast<fooB*>(GetProcAddr( hDllB, L"fooB" ));
  fooBB_ptr = reinterpret_cast<fooBB*>(GetProcAddr( hDllB, L"fooBB" ));
}

void done()
{
  FreeLibrary( hDllB );
}

void fooA()
{
  fooBB_ptr();
}
void fooAA()
{
  fooB_ptr();
}

Same in DLL B.

Kirill V. Lyadvinsky
A: 

Do you actually rebuild both DLLs each time you compile ?

If not, the import libraries should be created the first time - even if the link phase fails because of unresolved externals - and allow subsequent builds to link without error...

edit: we actually have that kind of dependencies in my shop. Our build is incremental indeed, and clean builds need some projects to be built several times in order to link succesfully.

RaphaelSP
That's what I want to avoir right now, I would like a way to only build one time on full build.
Drahakar
Well, if incremental build is *really* not an option, I don't know of any other way than the one jia3ep suggests. I guess you could also change one of the DLLs to a static library.
RaphaelSP
Wouldn' the keyword external be of anyuse? I know it work between two .obj, but does it work between two project?Or would a manual compile (/c on VS) with a manual /LINK be an option? if not why?
Drahakar
If by "external" you mean "extern", then no: although it does have affect the visibility of a symbol, it will not help situations where the exported symbols table does not exist yet.
RaphaelSP