11

I ran into a strange problem with a Visual Studio 2008 project I was working with recently.

I am trying to compile a new static library that uses functions from another static library. (Let's say Lib1 is my static library project, and Lib2 is the lib file that Lib1 depends on).

I am able to build lib1 without issue; It includes the header files for lib2 and calls its functions, and there are no problems.

The problem is when I build a separate test project that has Lib1 as a dependency; it won't build and I get linker errors. The unresolved externals are the functions I am trying to call within Lib1 that are from Lib2.

This is all fixed when I include Lib2 in my test project as well.

This all makes sense to me of course; I can test that Lib2 is not being built into Lib1..

My question is: is there a way to do this? I would ideally like to be able to deploy Lib1 as a standalone lib without requiring Lib2. (Lib2 is actually just a Lib from the Windows Platform SDK, so it's not really a big deal...)

Is this not allowed because it would allow people to "hide" third party libraries in their own, or something?

What would be a professional approach to this problem?

Thanks!

--R

3
  • Did you fully compile your Lib1 before including it in your project? Commented Mar 26, 2011 at 20:39
  • Did you solve the issue? What did yo do? Commented Feb 24, 2015 at 20:01
  • Honestly, I don't recall; this was a long time ago. I think I simply documented the dependency, but I don't remember specifics, sorry. Commented Feb 24, 2015 at 20:36

6 Answers 6

3

I would not advise using a librarian to take Windows' library contents into your own library -- it's likely that that's against the license.

I see two possibilities

  1. Documenting the dependency
  2. Using a #pragma in your .h file that requests the .lib to be linked against. If VS can find it, it's the same as including it on your link line.

http://msdn.microsoft.com/en-us/library/7f0aews7(VS.80).aspx

 #pragma comment(lib, "libname.lib") 
Sign up to request clarification or add additional context in comments.

4 Comments

What are you saying? this can not be done? Thank You.
All I said is that if the library is from Microsoft (like in this question) it is likely against the license to distribute it this way. I didn't comment on whether it was technically feasible or not.
What if it is a Library I have license to distribute? How can that be done? Thank You.
Read the other answers, there are links like this: support.microsoft.com/kb/31339
3

You need to use a tool called a librarian to do this. A librarian allows you to create and modify library (.lib) files. In visual studio check under the Librarian section of your project properties. A command line version also comes with visual studio (lib.exe).

2 Comments

This is technically correct, but it's likely that Windows Platform SDK's prohibit this in their license.
@Ferruccio, I tried the flag suggested in that question: stackoverflow.com/a/18200652/195787. Yet still when I tried compiling a new project using the stacked Library I get an error in the linker about H file from the static library. I don't get it...
2

Just document the dependencies of your lib.

As long as the library you depend on is available to anyone that could use your library, this is the preferred solution. Especially considering that the library user could also depend on this platform SDK lib - if you had it embedded then he'd get funny linker errors with multiply defined symbols.

Comments

1

This is a fairly normal problem - you wouldn't normally attempt to include 'lib2' into 'lib1' but simply document that it's required to be linked against in order to work. There is nothing wrong with declaring the use of other libraries (apart from any licensing issues of course) so you are already doing the right thing.

Comments

1

If you really want to do this, you can extract the .obj files from Lib2 and add them to Lib1.

See How to Extract .OBJ Routines from .LIB Files Using LIB.EXE -- I hope it is still relevant for VS2008.

Comments

1

Instead of simply documenting your dependencies, use #pragma comment(lib, 'lib2name') in your code to make the linker pull in the other library automatically. Since you said you're using a standard library that comes with the SDK, this should eliminate all burden on the application.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.