How to Include Libraries in Visual Studio 2012

How to include libraries in Visual Studio 2012?

Typically you need to do 5 things to include a library in your project:

1) Add #include statements necessary files with declarations/interfaces, e.g.:

#include "library.h"

2) Add an include directory for the compiler to look into

-> Configuration Properties/VC++ Directories/Include Directories (click and edit, add a new entry)

3) Add a library directory for *.lib files:

-> project(on top bar)/properties/Configuration Properties/VC++ Directories/Library Directories (click and edit, add a new entry)

4) Link the lib's *.lib files

-> Configuration Properties/Linker/Input/Additional Dependencies (e.g.: library.lib;

5) Place *.dll files either:

-> in the directory you'll be opening your final executable from or into Windows/system32

How to install C++ library to VS2012?

If it's a static library

1) Go to Project Properties -> Linker -> General -> Additional Library Directories -> Place here the path of your library ( .lib ) file

2) Project Properties -> Linker -> Input -> Additional Dependencies -> Place the name of your .lib file here.

If it's a dynamic link library

1) Go to Project Properties -> Linker -> General -> Additional Library Directories -> Place here the path of your library ( .lib ) file

2) Project Properties -> Linker -> Input -> Additional Dependencies -> Place the name of your .lib file here.

3) Now when you run the exe make sure that your .dll file is in the current directory where you run the exe or make sure the path of the .dll is in the environment path.

Hope this helps in your case.

C++, Visual Studio 2012, LIBs, DLLs, Includes, Source and Linker

  1. You don't need to have LIB files along with your EXE file for running in another computer, LIB files are static files and DLL files are dynamic. So when you compile all static codes will be included in your EXE file, but DLL files will be loaded and used dynamically in runtime, so you just need to have your DLL files with your EXE file. This way, your code will work and run properly in other computers.

  2. Just adding another project is not enough, you need to compile them and generate LIB files out of them. Then you add the generated LIB file to your final project and include external projects in your final binary. If you are compiling multiple projects together in a solution, you'll need to set project build order in solution properties in VS.

  3. No, that's OK. It seems you've put LIB files in right folder and you don't need to have LIB file with your EXE file to run it in other computers.

  4. DLLs are dynamic libraries, so you need to have them with your application. Installers usually install EXE files with DLL files in the same folder, so your app will run properly, but no need to include LIB files at all.

Also you can include LIB files like this:

#pragma comment(lib, "glew32.lib")

So you don't need to do it in project settings, but assuming you have your LIB file in "Library Directories" path.

Using DLL files can be done in two ways:
One is linking your application to DLL file and having DLL file's function entry in your EXE file's import table:
like using

#include <windows.h>

then

GetWindowsDirectory(windir, MAX_PATH);

So you'll have GetWindowsDirectory API entry in your EXE file's Import Table.

Also you can do it dynamically:

hinstDLL = LoadLibrary("kernel32.dll");
if (hinstDLL != NULL)
{
func_GetWindir = (DLLPROC) GetProcAddress(hinstDLL, "GetWindowsDirectoryA");
...

There is not much difference, only difference is:
In first method, as it's in your EXE file's Import Table, if there was no kernel32.dll or there was no GetWindowsDirectory entry in kernel32.dll, your EXE will not run at all, it will show a critical error and will not run. But in dynamic way (second way), your app will run, but as soon as your code try to use GetWindowsDirectoryA API, it will fail. You will have 0x00 in func_GetWindir. If you attempt to call it, then program will crash.

How to add additional libraries to Visual Studio project?

For Visual Studio you'll want to right click on your project in the solution explorer and then click on Properties.

Next open Configuration Properties and then Linker.

Now you want to add the folder you have the Allegro libraries in to Additional Library Directories,

Linker -> Input you'll add the actual library files under Additional Dependencies.

For the Header Files you'll also want to include their directories under C/C++ -> Additional Include Directories.

If there is a dll have a copy of it in your main project folder, and done.

I would recommend putting the Allegro files in the your project folder and then using local references in for the library and header directories.

Doing this will allow you to run the application on other computers without having to install Allergo on the other computer.

This was written for Visual Studio 2008. For 2010 it should be roughly the same.

Visual Studio 2012 link errors with static libraries

Visual Studio 2005 did some magic with project dependencies where it would automatically link in any .lib outputs (I unfortunately was the developer that helped implement it). This appears to have been removed since, I suspect, Visual Studio 2010 when the old Visual C++ build system was replaced with MSBuild.

However, the "automatic linking of static library dependencies" feature can still be found via project references:

  • Right click on the App project and select "References..."
  • Click "Add New Reference".
  • Check the static library project and press OK.
  • Build.

You should now see the static library being automatically linked in. Note that project references also imply a project dependency.

If you prefer to use a project dependency instead, you'll need to add the static library to the linker's additional dependencies property on the "App" project, like you would for any other static library input.

Edit: also, you'll see a property called "Link Library Dependencies" on the project reference. This controls whether or not the .lib output of the referenced project gets linked in or not (defaults to true).

How to link against .a files in Visual Studio 2012?

  • Include the .h header files, and make sure they are present in your project
  • Go to Project Properties > Configuration Properties > C/C++ > General > Additional Include Directories and add the path to the include directory where your header files can be found
  • Go to Project Properties > Configuration Properties > Linker > General > Additional Library Directories and add the path to the lib directory where your .a files can be found
  • Go to Project Properties > Configuration Properties > Linker > Input > Additional Dependencies and add the .a file accurately

Apart from this make sure you are in the correct configuration platform while building (x86 vs x64) and it is same as the one your library uses.



Related Topics



Leave a reply



Submit