13

Situation:

I'm building a library with VS2010, and it has a dependency on LibraryA. I am only using one of many features that LibraryA offers, so I want to link it in statically.

Everything I'm reading about this tells me to set the whole project to link statically against MFC, which is something I don't want to do. I'm just fine with my library dynamically linking against windows DLLs; I just want to statically link against LibraryA only.

Fooling around with the options windows, I don't seem to see such an option. Is it really all or nothing?

Thanks, -Ben

Ben
  • 7,692
  • 15
  • 49
  • 64
  • "link statically against MVC" is hard to interpret. Do you mean the CRT? Only the /MD vs /MT option matters. The Windows DLLs are not an issue and are always dynamically linked, there is no other option. – Hans Passant Dec 20 '11 at 20:11
  • Does libraryA come as a dll(with a lib to link) or just a lib? Some 3rd party libraries come as both so you can choose the way you wish to link it. If it is your library/code you can choose how you build and link it. – Shaun Wilde Dec 20 '11 at 20:17
  • @HansPassant - My mistake (it was MFC). See http://stackoverflow.com/questions/3230/how-do-you-pack-a-visual-studio-c-project-for-release - The top answer there talks about it. Its close to my situation, but not the same. – Ben Dec 20 '11 at 20:41
  • @ShaunWilde - LibraryA is something I'm building from source, so I can go either way with it. Correct me if I'm wrong here, but don't I need it as a .lib in order to statically link against it in my project? Assuming that is true, then I should be able to statically link it, but I can't seem to find the option to just statically link against this one particular library, without changing how I link against MFC stuff. – Ben Dec 20 '11 at 20:43

1 Answers1

30

Answering my own question here:

All you need to do to statically link a library in VS is:

1) Add the .lib file to the list found in properties -> linker -> input : Additional Dependencies.

2) Add the directory that the .lib file is located at to the properties -> linker -> general : Additional Library Directories.

If the .lib file is a statically linked library, then that is all you have to do.

The main reason I was confused was that a .lib file could also be a companion file alongside a dll, and not a static library itself.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Ben
  • 7,692
  • 15
  • 49
  • 64
  • 6
    You're not an idiot, you're (probably) human. Note that if you're building both projects at the same time (ie. in the same solution), you can set one project to depend on the other and Visual Studio will automatically import the library without the need for explicitly adding it to "Additional Dependencies". – adelphus Jan 09 '12 at 16:24
  • This post helped me too: http://stackoverflow.com/questions/6402586/know-if-lib-is-static-or-import – meawoppl May 01 '14 at 15:16