How do you add external libraries for compilation in VC++?

85,447

Solution 1

In I think you might be asking the mechanics of how to add a lib to a project/solution in the IDEs...

In 2003, 2005 and 2008 it is something similar to:

from the solution explorer - right click on the project select properties (typically last one) I usually select all configurations at the top... Linker Input

Additional dependencies go in there

I wish I could do a screen capture for this.

In VC6 it is different bear with me as this is all from memory

project settings or properties and then go to the linker tab and find where the libs can be added.

Please excuse the haphazard nature of this post. I think that is what you want though.

Solution 2

Libraries in C++ are also considered helpful, but the way you integrate them is different to Java because the compiler only has to see the interface of the library, which is usually declared in header files. In Java, the compiler will have to inspect the actual libraries because Java doesn't have this distinction between an externally visible header file and the generated object code providing the implementation.

What you normally do is build the libraries separately, once, and put the generated lib/dll files plus the header files into a place that projects requiring the library can access. A common idiom is to put the header files into include, the static libraries into lib and the dynamic libraries into bin subdirectories for your compiled library.

The reason you have found that most C++ libraries are provided in source code form and not in precompiled form is that every C++ compiler has a certain freedom as to how to mangle symbol names etc and the resulting object code isn't portable across compilers, let alone operating systems. So shipping the compiled code doesn't make sense for a lot of applications. You'll occasionally find it with closed-source C++ libraries on Windows (C libraries are an entirely different matter), but then the vendor will have to provide a compiled version for each and every build type (Release, Debug, 32 bit, 64 bit etc) and target compiler (various versions of Visual Studio require different binaries, then there is Borland and a bunch of other compilers) and it quickly becomes a nightmare to support...

When you take a library and build it as a dynamic library on Windows (ie, a DLL), the compiler/linker will normally generate a static 'import' library for it (same name, just with a .lib extension). When you link your project against the dynamic library, you specify the .lib file as a library dependency. Linking your application against said import library allows the linker to record the dependency on the .dll file and also which symbols it should expect the library to provide.

Making them work - in the sense of your program finding them on Windows - usually requires that the .dll file is either in the same directory as the executable or accessible via the 'PATH' environment variable and its equivalent in Visual C++.

Solution 3

Ok. typically you don't want to load dynamic libraries by hand, but if you do, look into LoadLibrary. you then have to call other functions to get function pointer addresses and so forth. Typically how it works, is even .dll files have .lib files for them.. so when they are needed, they just automatically load.

how you specifiy .lib files /static libraries is under Properties/Linker/Input->Additional Dependencies. in the gui.

if you are scripting this.. well. you just specifiy the .lib files on the command line at link time.

Solution 4

For adding libraries, this is very simple (if that's what you mean) Project -> properties -> configure properties -> Linker -> Input -> additional libraries. Go stand on one of the libraries in there and press enter. You freed up a space to add a library, eay as pie :)

Solution 5

For adding external libraries or library directories in a VC++ project follow these steps:
1- Go to your project properties (that can be done by right clicking on the project in your solution)
2- Select 'Linker' > 'General' > Additional Library Directories
3- Click on the empty bar in front of 'Additional Library Directories' and VS allows you to add directories to its default ones into which it searches for the required libraries.

Share:
85,447
Jamie Deakin
Author by

Jamie Deakin

Projects jobdb - Creator of Open Source Job Search Document Creator/Tracker http://i9.photobucket.com/albums/a58/Maskkkk/c64nMe.jpg Received my first computer (see above) at the age of 3, wrote my first program at the age of 7. Been hooked on programming ever since.

Updated on July 09, 2022

Comments

  • Jamie Deakin
    Jamie Deakin almost 2 years

    I've worked with a couple of Visual C++ compilers (VC97, VC2005, VC2008) and I haven't really found a clearcut way of adding external libraries to my builds. I come from a Java background, and in Java libraries are everything!

    I understand from compiling open-source projects on my Linux box that all the source code for the library seems to need to be included, with the exception of those .so files.

    Also I've heard of the .lib static libraries and .dll dynamic libraries, but I'm still not entirely sure how to add them to a build and make them work. How does one go about this?

  • IInspectable
    IInspectable about 10 years
    -1 Libraries that are distributed as binaries come with an accompanying import library (.lib). Name mangling is completely irrelevant here, as the symbol names are listed in the .lib file. Whether those symbols are a compiler's mangled (C++) names, a vendor's export names as listed in a .def file, or the (re-)named symbols as assigned by the developer on the linker's command line (/EXPORT) does not make a difference. The symbol names are readily available to any linker.
  • Jørgen Fogh
    Jørgen Fogh about 8 years
    @IInspectable: Name mangling may be irrelevant but the calling conventions and runtime library is not. If you are not careful when you design the library, you can easily tie the binary to a specific compiler version.