Pepor
Pepor

Reputation: 1072

How to set up nested library dependencies?

We have a rather large solution (consisting of 40 projects total) that I recently upgraded to Visual Studio 2010. While upgrading, I thought it might be a good idea to clean up project dependencies. Which is what I cannot seem to get right.

The main .exe (C#) depends on a C++/CLI project that wraps an unmanaged C++ library project, which itself depends on several third-party libraries. So the dependency tree would look something like this:

To have everything nice and tidy, I'd like the LibWrapper to only have a dependency on CoreLib, while CoreLib references all its dependencies. I set up the project dependencies accordingly, and everything works fine when rebuilding the solution from scratch. So far, so good.

However, when I change something in LibWrapper and then try to build the solution (and only LibWrapper is recompiled and -linked), the linker doesn't pick up the nested dependencies (libjpeg and the others), and thus throws a gazillion of linker errors. This is technically correct, as the linker cannot find the symbols because it doesn't look at the nested libs at all. But why does it work as expected when rebuilding everything?

There's probably something with the library dependency settings, but I cannot figure out what. I could add the CoreLib dependencies to LibWrapper, but that is sort of ugly IMHO.

For the libraries that CoreLib depends on, I set "Reference assembly output" and "Link library dependencies" to false. For the CoreLib dependency in LibWrapper, I set both to true.

Update I have found a rather ugly workaround that at least works, though it's not very nice to maintain. So for each lib project that has dependencies, I added a header file that lists these dependencies. So, for CoreLib, for example, the file looks like this:

// NestedDependencies.h

#pragma once

// add nested dependencies
#pragma comment(lib, "libjpeg")
#pragma comment(lib, "libpng")
// more libs..

On the dependent project, I include this header from the stdafx.h (or another central location):

// stdafx.h

#pragma once

#using <mscorlib.dll>
#include <vcclr.h>
#include <windows.h>
#include <atlbase.h>
// whatever else

// add nested dependencies
#include "CoreLib/NestedDependencies.h"

At least this works, though everyone on our team has to remember to add a line to NestedDependencies.h whenever a library dependency is added. This is not nice, but workable.

However, I'd still prefer a better solution. Does anyone know?

Upvotes: 3

Views: 1704

Answers (1)

Morten Fjeldstad
Morten Fjeldstad

Reputation: 903

Change Core.lib to a DLL, otherwise the correct aproach is to explicitly link all dependencies in the LibWrapper.DLL as far as I know.

Somewhat related as it deal with why.

Upvotes: 0

Related Questions