Referenced libraries in Visual Studio 2010 projects - c++

Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one main project with the program entry point and a second project with a static library (linked against the main one), that is used in other VS2010 solutions.
The static library project uses an additional third-party library (.lib). Currently, this third-party static library is set in the main project's Additional Dependencies field of the Linker -> Input section of the Property Pages, in order for the whole program to compile.
My question is, is there a way to add this dependency in the static library project itself, so I don't have to add it manually in each of the main projects from the different VS2010 solutions where the static library project is included?
Are there different ways to do this? Can I chose embed this third-party library in the .lib generated by the static library project? Is there any good practice or recommendations on doing this?
Thanks!

A static library is just like any other output of the linker - you can add additional dependencies in the 'librarian options' of your static lib project and they will be linked in as normal.

Your static library project uses a third party static library. You only need to link the library when you create your static library, and your static library dont have to ship the third part with it, but just your library. Your static library embeds all the required binary parts from the third party static library to your static library.
If you are not using any of stuff from the third party library (i.e. if your static library has the functionality for all other projects require ) in other projects then you dont have to link aganist the third party static library. Just use your static libray instead.

Related

How to link a *lib [duplicate]

There are things that I don't understand when it comes to linking... I'm writing a program using a 3rd party library (the GEOS library). This program has a dependency to geos.lib but still needs geos.dll to run.
I read this question, I think I understand the difference between static and dynamic libraries. What I don't understand is why I still need a dll when I statically link a library.
There are 3 kinds of libraries on Windows:
object library (*.lib)
import library (*.lib)
dynamic library (*.dll)
object libraries are statically linked. They contain the full object definitions of the code abstracted by the library.
import libraries is a special form of an object library. Instead of containing code they contain information for the linker that ultimately maps the executable file to the dynamic-link library.
dynamic link libraries, like object libraries, supply code for your program. However, this code is loaded at runtime and not compiled into your exe.
You don't always need to link an import library. Instead you can call LoadLibrary() and lookup the API entry points by name or ordinal. (You always have to tell the code which DLL and where in that DLL's API you want to enter.)
The other comments here are correct in that you cannot make a DLL into a static lib without recompiling the code for the libary -- it is a different kind of output.
It's not statically linked. The .lib is just a stub library that binds in the .dll on windows. That is, you link with the .lib at compile time, and then at runtime it will go looking for the .dll.
If .lib was created by Visual Studio then check value of Project properties -> Linker -> Input -> Module Definition File. If it's not empty then link.exe create stub library instead of static library even if Project properties -> General -> Configuration Type is "Static library (.lib)".
You are definetely linking to a dynamic library.
Just because the linker requires .lib file doesn't mean you're linking to a static library.
You can statically link the lib file if and only if this is a static lib file. So first you need to convert your dll's project to the static lib, build it and after that use the product of your build which will be a static .lib file.

Compile project with static and dynamic linked libs

I'm happy with dynamic linking while creating small c++ windows applications. But now I need to add a third part library. I have both a dll and a lib file. I prefer not to copy third party dll each time I install my application. Can I somehow link newly provided library statically while keeping whole project dynamically linked? In Microsoft Visual Studio IDE I found only one place where I can choose static/dynamic linking, in Properties->Configuration Properties->Code Generation->Runtime Library. But I suppose this selection is for the whole project and I need to link some libs statically and some of them dynamically.
UPDATE
In general I need to link some libs dynamically and others statically in the same project. The provided link tells only about static or dynamic linking in the same project

Program statically linked to a library but still needs dll to run

There are things that I don't understand when it comes to linking... I'm writing a program using a 3rd party library (the GEOS library). This program has a dependency to geos.lib but still needs geos.dll to run.
I read this question, I think I understand the difference between static and dynamic libraries. What I don't understand is why I still need a dll when I statically link a library.
There are 3 kinds of libraries on Windows:
object library (*.lib)
import library (*.lib)
dynamic library (*.dll)
object libraries are statically linked. They contain the full object definitions of the code abstracted by the library.
import libraries is a special form of an object library. Instead of containing code they contain information for the linker that ultimately maps the executable file to the dynamic-link library.
dynamic link libraries, like object libraries, supply code for your program. However, this code is loaded at runtime and not compiled into your exe.
You don't always need to link an import library. Instead you can call LoadLibrary() and lookup the API entry points by name or ordinal. (You always have to tell the code which DLL and where in that DLL's API you want to enter.)
The other comments here are correct in that you cannot make a DLL into a static lib without recompiling the code for the libary -- it is a different kind of output.
It's not statically linked. The .lib is just a stub library that binds in the .dll on windows. That is, you link with the .lib at compile time, and then at runtime it will go looking for the .dll.
If .lib was created by Visual Studio then check value of Project properties -> Linker -> Input -> Module Definition File. If it's not empty then link.exe create stub library instead of static library even if Project properties -> General -> Configuration Type is "Static library (.lib)".
You are definetely linking to a dynamic library.
Just because the linker requires .lib file doesn't mean you're linking to a static library.
You can statically link the lib file if and only if this is a static lib file. So first you need to convert your dll's project to the static lib, build it and after that use the product of your build which will be a static .lib file.

Visual Studio: How to specify different runtime libraries for the linker? (/MTd, MDd, etc)

I'm linking to a few libraries in VS2008. If my knowledge of the linker is correct, MTd is for static linking and MDd is for dynamic linking (to a DLL.) My goal is to statically link some libraries and dynamic link others. The project options seems to only have one setting for all libraries in the linker input. How would I do this?
Your project will be given a sensible C Runtime Library default after you set it up, depending on how you answer the New Project Wizard prompts. You can inspect and alter this (if needed) as follows:
right-click the relevant project in Solution Explorer, select Properties
look under Configuration Properties, C/C++, Code Generation, Runtime Library.
Other libraries can be linked however you want, you just specify the library to link to under Linker, Input, Additional Dependencies.
Even if you are linking to a DLL, it will still have a .LIB file (of the correct form for a DLL) to resolve external references, unless you are manually loading the DLL and discovering required function entry points.
You do need to make sure that the LIB files you link to use the same CRT as your app does, or things can go unexpectedly wrong.
No, you're mixing it up. The /MD vs /MT options is only relevant to which CRT version you link. There are two, the static version (/MT) which you should use only if you don't use any DLLs in your project. And the DLL version, a version that every binary in your process can share so that you won't have heap allocation misery. The kind of misery you get into when memory is allocated by one module and freed by another.
Choosing your own libraries is entirely up to you. Mixing and matching is fine, the linker just gets another kind of .lib. An import library instead of a static library. Just keep in mind to use /MD when you use DLLs.
The linker options your describing are for the CRT only. The static version will limit how you share memory between modules.
All other libraries you use will have be linked in based on the .lib files you provide(or not) to the linker.
There are 3 ways to use a MS library
statically link static library (.lib equivelant of an .a archive of .o)
statically link the stub (.lib compiler generated loadlib/getproc) of a dynamic library
manually load a dynamic library (loadlib/getprocaddress)

two static libraries

I am currently providing a static library using vs2008. I am in the process of building my static library. However, since I am using another static library is there a way that i package this as a single static library. The reason here is that they will be calling functions in my library that depend on that other static library (.lib). I am not sure how to go about doing that and need some help with that.
Here is one way:
Find out all the object files in the static library. That can be done by running the command lib STATICLIB /list
Extract each object listed. You must give the exact name from step 1 (lib STATICLIB /extract:.\debug\foo.obj)
You can then add all the objects extracted form step 2 into your library
Look under project properties | Configuration Properties | Librarian | General
Put the name of the library (.lib) file you want to add to your library under "Additional Dependencies"
You may also have to set the "Additional Library Directories" setting so that it can find the library.
VC++ includes a command line library manager called lib.exe. It can me used to combine both object files and library files into a single library.
It may not be relevant to your case, but in some cases combining a third-party library to your own and distributing that as a library rather than a final application, (or in the case of many open source licenses, the source), may contravene the license terms for that library. So apply some caution in such cases.