How to add static libraries to a Visual studio project - c++

I am trying to add static libraries to my project. To add the static library I am following Microsoft's instructions: http://msdn.microsoft.com/en-us/library/ms235627.aspx.
My problem is I that am not able to see the dependent library while adding the reference to my project.
In the tutorial mentioned above, they have mentioned that the dependency (ie. static library), should be added to the solution.

The tutorial you have provided refers to a case in which you create your own static library - in this case you may want to add it to your solution and thus make it an integral part of the solution; but I assume you are trying to add external libraries (not created by you, e.g. downloaded from the net) - that is why you got stuck.
On Property Pages, go to C/C++->General->Additional Include Directories and provide the path, where the header file of the library that you want to use is located.
Then go to Linker->General->Additional Library Directories and specify the path, where your .lib file is located.
Finally, go to Linker->Input->Additional Dependencies and add the name of the file containing your library together with its extension (e.g. example_library.lib).
That is all. Now you should be able to use the library. Remember to #include it in your files and use the right mode (release/debug) and the right version for your platform (x64/win32). You may have to repeat the steps given above both for release and debug versions of your app.

I am just extending the answer given by KjMag. It's a great answer, except that it misses the part where we tell the linker which external libraries to add.
In Visual Studio, go to Property Pages >> Linker >> Input >> Additional Dependencies. Here we can add the required libraries.

Related

How can I install and use C++ libraries on eclipse?

I want to use libosmium library. Could someone please tell me how do I set up this library after I download it?
All I need to know is the standard way of installing external libraries. I can't really find very clear instructions online.
I'm coding with C++ using the eclipse IDE version 4.18.0.
For includes: right click on your project and go to Properties>>C/C++ Build>>Settings>>GCC C++ Compiler >> Includes
you must add the include path for the external library (where the header reside). This information is needed by the Eclipse indexer (code completion etc.) and the compiler
For libraries: right click on your project and go to Properties>>C/C++ Build>>Settings>>GCC C++ Linker >> Libraries you must add the library search path (option -L) and the library you want to link against (option -l). This info is needed for the linker.
Source: http://wiki.eclipse.org/CDT/User/FAQ#How_do_I_add_an_external_library_to_my_C.2B.2B_project.3F
Eclipse-CDT Setting Pictures
Include path settings:
Library & library search path settings
You can also use pkg-config plug in
https://marketplace.eclipse.org/content/pkg-config-support-eclipse-cdt
Here is a link to a similar question with answers: Problems importing libraries to my c++ project, how to fix this?
As specified in the other answer, you can usually add a library by looking at the project properties:
Properties>>C/C++ Build>>Settings>>GCC C++ Compiler>>Includes
However if you're using a makefile project, the Makefile itself must know about the location of libraries. In this case, in order to avoid an "Unresolved inclusion" warning in the header, you may also want to tell eclipse where the header files are. This way the autocomplete & "Open Declaration" will work. This is found in:
Properties>>C++ General>>Preprocessor Includes Paths, Macros etc.
In my case I couldn't figure out how to add a custom configuration and updated the default configuration. I added a path for the Android NDK under the GNU C++ -> CTD User Setting Entries

cannot open file 'SDL2.lib'

I am trying to open sdl2 in my project on Visual C++ 2012 but i keep getting this error:
error LNK1104: cannot open file 'SDL2.lib'
I added the additional libraries and include folder but no luck..
I added the link to the libraries through the linker but i get the same error..
what should i do?
I'm referencing VS 2010, but I've read before that some people have had errors when using the VC++ Directories in their project properties as opposed to the Linker->General->Additional Library Directories.
Unfortunately, there really isn't much more to do besides making sure the directories are linked properly. There really is only one answer to this question. It also may depend on the download of SDL you chose, are you donwloading the Development Library as opposed to the binary?
Watch this video https://www.youtube.com/watch?v=or1dAmUO8k0.
In summary, you want to add the path include folder here: (configuration properties> C/C++ > General> Additional Include Directories),
and add the path of the lib folder here: (configuration properties> Linker > General> Additional Library Directories)
and finally, link .lib file in: linker > input > Additional Dependencies.
It seems like you didn't add your library directory path in VC++ Directories/Library Directories.. You have to link your library directory path (Where you have save you libraries of SDL) in your program. After adding, it will definitely work
I would like to add for others experiencing this problem that its an easy mistake if you are using the Development Library to link to the lib folder. SDL needs you link to either the x86 or x64 folder in the lib folder.
If you run or compile your program before you make those required changes of SDL then you will get this error always.. so first add a c++ file then make those changes and then write something and try to run ..this time it will never give this error
It seems like VS can't find the lib files. There are two ways you can do this. One is to configure the appropriate directories in VS as TwinkleBearDev's article shows. Another is to put the libraries directly where VS would look for them, as my article shows. I don't know if the folders for VS2012 are the same though - might be slightly different.

How to use my library in C++?

In Eclipse I have created two libraries. One of them is shared another one is static. I have compiled them in Eclipse and as a result a Debug folder was created (for both libraries) and these folders contain make-files as well as object files (*.o) and dependency reference file (*.d). In addition to that, the static library contains an *.a file.
Now I create a new project and what to use these library in this project. Normally, when I use a library I type #include <libraryname>. But if I use #include <mylibraryname> it does not work (I get unresolved inclusion). And this is not surprising because Eclipse should somehow know where my library is located. So, my question is how can I inform Eclipse about the locations of my libraries.
ADDED As recommended I do the following sequence "Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries". Then, in the "libraries(-l)" filed I add "StaticList" (because I have "libStaticList.a" file) and in the "Library search path (-L)" filed I give the full name of the directory where my "libStaticList.a" is located. Then I click Apply and OK. But it does not help. Eclipse does not like #include <StaticList>. It complains: "Unresolved Inclusion..".
#includeing headers only makes the compiler aware that the functions in those headers exist. The actual implementation of those functions needs to be linked in by the linker. That's where the library (.a) files that you built come in. Check out this thread for an example on how to link in your libraries using Eclipse.
I think you need to #include "yourlibrary.cpp" (between double quotes) instead of < >.

Why is VisualStudio looking for this lib file? LNK1104 error

We have a large project using VS2008 and boost 1_42. I'm trying to upgrade to VS2010 and boost 1_44. I installed VS2010 and boost 1_44 and converted the project. Now I am trying to build, and everything compiles, but fails when linking:
LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc90-mt-1_42.lib'
I have changed the include and lib directories to point to the new boost 1_44 files and I have renamed the old boost 1_42 directory.
Why is the linker still looking for a vc90-1_42 file, when it is only using 1_44 headers? Is there a way that I can determine WHY the linker wants this file? The linker obviously thinks it needs the file, but why?
I have cleaned the project and I am re-building to ensure any old build files are erased.
I've run into exactly this problem a couple of times too. It's usually been some old temporary files but like in your case cleaning didn't always do the trick straight away. Does your project include any static libs that might have been built with 1.42?
Something you can try which may or may not be helpful in tracking down your issue:
Rename the old boost directory back to it's original name
Clean the solution
Under C/C++->Command Line->Additional Options add "/showIncludes"
Under Linker->Command Line->Additional Options add "/verbose:lib"
Rebuild all
Then when you build you'll be able to see at which point 1.42 headers are included, etc. in the output window. Somehow doing this helped me in tracking down where the problem was.
Along with changing the lib directory, you need to change the name of the boost library. That's in the Linker | Input section of the project settings.
Your added comment makes it clear that the dependency on the Boost 1.42 library was being created indirectly by another library that hadn't been rebuilt.
For this you basically have two choices: either add that library as a project to your main solution, and make sure it has enough dependency information that it'll be re-built when you upgrade Boost, or use the /Zl compiler switch when you build your library. This tells the compiler you're building a library so you do not want to embed library dependencies like this.
Boost uses
#pragma comment(lib)
command to inform the linker of libraries it needs to link with. It is not an error. If Boost says you need it, it's likely you do.
On How can I find out why the linker wants this file?
There are programs which will go through your app and dlls/libs and report the content of manifests and what the binaries report they depend on. You could then scan the report for the unexpected libraries being included. We used this mainly to find libs including the previous version of the VC runtime.
Have not used the one we had in about 5 years though, now if only I could remember the name of the app!
DependancyWalker (depends.exe) will allow you to see dependancies of dll/exe but not static libs.
You could open each binary as a 'file' in MSVS and look at the manifest content by hand, but I imaging this would be a bit painful. I've not tried this with a static lib.

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.