Montaldo
Montaldo

Reputation: 863

How to link your external library dependencies into one static library file in VS2015

This is the situation.

I have a library project A that uses and manipulates other third party libraries, we call them T1 and T2.

However when I use this library A in any of my other projects it complains that it cannot open the third party libraries T1 and T2 even though it does not directly uses them. Ofcourse it indirectly uses them through library A because A did the appropriate work to link and include against that library etc.

Do I really need to add these other libraries to the projects that use library A or is there a way to lets say concatenate libraries such that the user of library A only ever needs to worry about that one instead of what its made of.

Edit:

Found some related questions but the answers do not quite solve the issue;

linking-static-libraries-to-other-static-libraries

how-to-combine-several-c-c-libraries-into-one

Edit2:

Thanks for the answers so far. Let me clearify my situation.

I have a .lib project in VS2015 which we call MathLib. This MathLib uses lets say a boost library internally to do its calculations, lets call this library BoostMath. The property files are all set to include and use this BoostMath and this works perfectly in the library project itself.

I am now making another project User that uses the MathLib to do its calculations. It does the appropriate includes and all to use this library. However it complains about the fact that it does not know the BoostMath library and thus cannot work with the MathLib library.

One could argue why not just include BoostMath into project User in the same manner that the MathLib library did this, but that is missing the point. I want to create a library of my own that may or may not use other libraries internally but this should not be of any concern to the end user of my library.

I probably have to set something in VS Librarian to make this happen, concatenate the libs together or some. But I cannot seem to figure it out. Any thoughts?

Edit3: I even found the exact same commandline in the property files as mentioned in this answer.

/OUT:"MathLib.lib" "BoostMath.lib" /NOLOGO /LIBPATH:"path\lib" 

However it does not work for some reason. If I run it with and without the Librian property setting, the .lib binary stays the same.

Appearantly this functionality is broken since VS2010? According to this answer. Usefull other question. Edit4:

I basically want to do this, but it does not seem to work in VS2015

+---------------+
| End user exe  |
+---------------+
       |
       | some api call
       |
+---------------+
| My MathLib    |
+---------------+
       |
       |
       +---------------+------------+----
       |               |            |
 +-----+------+  +-----+-----+
 | BoostMath  |  | OtherMath |
 +------------+  +-----------+

Upvotes: 4

Views: 3811

Answers (2)

Ofek Shilon
Ofek Shilon

Reputation: 16217

Basically static libraries do not undergo linkage, and in particular static libraries have no way to specify their own dependencies. You can partially hack around this by spreading #pragma comment(lib)'s around individual obj files (i.e., individual source files), but not at the library level - and it seems you don't intend to modify the lib sources anyway.

Your best option seems to be combining the lib along with its dependencies into a single lib, as specified e.g. here:

lib.exe /OUT:compositelib.lib lib1.lib lib2.lib

Note that you'd have to re-package whenever any of the dependencies change. I myself, as a user, would prefer to include all the referenced libs in my consumer solution.

Upvotes: 1

user3567728
user3567728

Reputation: 125

I am not sure how you link the library to the project...but it should not complain the compiler could not open the library T1 and T2 unless you have included those in the compiler setting.

Normally if you don't have the library, it will report could not find the function example T1_xxxx (this function is defined in T1). I would suggest you check the compiler setting of the project

Upvotes: 1

Related Questions