Search

A.2 — Using libraries with Visual Studio 2005 Express

To recap the process needed to use a library:

Once per library:
1) Acquire the library. Download it from the website or via a package manager.
2) Install the library. Unzip it to a directory or install it via a package manager.
3) Tell the compiler where to look for the header file(s) for the library.
4) Tell the linker where to look for the library file(s) for the library.

Once per project:
5) Tell the linker which static or import library files to link.
6) #include the library’s header file(s) in your program.
7) Make sure the program know where to find any dynamic libraries being used.

Steps 1 and 2 -- Acquire and install library

Download and install the library to your hard disk. See the tutorial on static and dynamic libraries for more information about this step.

Steps 3 and 4 -- Tell the compiler where to find headers and library files

We are going to do this on a global basis so the library will be available to all of our projects. Consequently, the following steps only need to be done once per library.

A) Go to the “Tools menu” and pick “Options”.

B) Open the “Projects and Solutions” node, and click on “VC++ Directories”.

C) In the upper right under “Show directories for:”, pick “Include Files”. Add the path to the .h files for the library.

D) In the upper right under “Show directories for:”, pick “Library Files”. Add the path to the .lib files for the library.

E) Click “OK”.

Step 5 -- Tell the linker which libraries your program is using

For step 5, we need to add .lib files from the library to our project. We do this on an individual project basis. Visual Studio offers us 3 different methods for adding .lib files to our project.

A) Use a #pragma preprocessor directive to your primary .cpp file. This solution only works with Visual Studio and is non-portable. Other compilers will ignore this line.

B) Add the .lib file to your project as if it were a .cpp or .h file. This solution works with Visual Studio, but not with many other compilers. This is the solution we recommend.

C) Add the library to the linker input. This is the most “portable” solution in the sense that every IDE will provide a similar mechanism. If you ever move to another compiler or IDE, this is the solution you will have to use. This solution requires 5 steps:

C-1) In the Solution Explorer, right click on the bolded project name and choose “Properties” from the menu.

C-2) Under the “Configuration:” dropdown, select “All Configurations”.

C-3) Open the “Configuration Properties” node, the “Linker” node, and click on “Input”.

C-4) Under “Additional Dependencies”, add the name of your library.

C-5) Click “OK”.

Steps 6 and 7 -- #include header files and make sure project can find DLLs

Simply #include the header file(s) from the library in your project.

See the tutorial A1 -- Static and dynamic libraries for more information step 7.

A.3 — Using libraries with Code::Blocks
Index
A.1 -- Static and dynamic libraries

11 comments to A.2 — Using libraries with Visual Studio 2005 Express

  • thanks a lot…solved my problem god bless u

  • amarjit

    just perfect…..keep up the good work

  • srinu

    thanks a lot dude..keep it up..god bles u..!!

  • m8ms

    I want to include an openSSL library
    it doesn’t have a .lib file so I get confused at 5.
    when I skip it I get:
    error LNK2001: unresolved external symbol _SSL_load_error_strings

    how do I include simple .h files in VS?

    ( I included extern “C” and it didn’t help)

  • Daniel

    Muito obrigado! Muito obrigado mesmo!!

  • Sharma Web Service offer you low price top quality Search Engine Optimization,Website Design,Development,website maintenance,Web Hosting Service India

  • Joe

    Great tutorials.

    What about an article on exporting functions from DLLs, rather than using a LIB file. Many DLLs seem to have undecorated (non-mangled) functions when listing them using dumpbin.exe.

    Sure you can do this using a extern ‘c’ directive, but that doesn’t work for classes (especially with their constructors/deconstructors since they have the same name).

    Some explanation of using MOdule Definition (*.def) files would also be helpful.

    Thanks

  • Anurag

    I am struggling in VS10 to do same. Please provide same steps using VS2010.

    Thanks!!!

  • Hi
    I use your post but LNK2019 and LNK 2011 error happened at function ArPrintString. how can i fix errors?

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter