How to add lib files and headers to a C++ Project - c++

I have been using libcurl in a C++ project.
I have added the libcurl Include and Library directory's to the VC++ Directories and added the .lib file to the Linkers Input Additional Dependencies.
Everything works fine but when I check in my code (TFS) and somebody gets it on another machine they cannot build etc due to not having libcurl installed on their machine or installed in different paths etc.
How do I add all the necessary files to my C++ Solution so that anyone getting the project from source control can build and link without error.
Thanks
The solution I went with was too add the library file to a folder within my solution, then add
$(SolutionDir)libs\curllib.lib
To the Additional Dependencies within Linker->Input

You can not add the library to the solution. you should distribute the library along the source code. Place it in a subdirectory of the project so that it goes along the whole project source. Then configure the path in Additional Library Dependencies. That way the people should be able to link appropriately when building from source.

Related

How to use an external library for C++ in VS2017

I've been programming in Python for over a year now but am just learning C++ and am unfamiliar with how to go about using external libraries, CMake and github for that matter. I'm trying to use an external library called cpr - https://github.com/whoshuu/cpr. So far I've followed the instructions in the 'Usage' section of that link up to, but not including, the "add_subdirectory(cpr)" bit.
So far I've got the source code for cpr in the Visual Studio project folder of my C++ project. In the project properties I've then added into Include Directories (under VC++ Directories) "$(SolutionDir)site_libs\cpr\include" and I've added the same thing into Additional Include Directories (under C/C++ -> All Options). This means that the following code compiles just fine:
#include <cpr/cpr.h>
int main(int argc, char** argv) {
auto r =
cpr::Get(cpr::Url{"https://api.github.com/repos/whoshuu/cpr/contributors"},
cpr::Authentication{"user", "pass"},
cpr::Parameters{{"anon", "true"}, {"key", "value"}});
r.status_code; // 200
r.header["content-type"]; // application/json; charset=utf-8
r.text; // JSON text string
}
However this code isn't working when it comes to building, due to link errors. I'm pretty sure the thing I'm missing is the actual .lib file for it to find where these functions etc are defined (nothing for cpr is set in the Linker -> Input property). So I'm wondering - how do I create this .lib file / is that even the right thing to do / what is this "add_subdirectory(cpr)" and where/how do I run it... basically what do I do to make this whole thing work..?! I've tried compiling cpr with CMake but it throws a load of errors about 'CMakeLists.txt' not present in certain folders.
Apologies if I've used any incorrect terminology here, only been learning C++ for a couple of days now. Any help massively appreciated!
If the library does not come as a binary distribution (that is with the .lib already built) you are going to need to build it as a separate project from the code you want to use the library, that step will build the .lib file. If a .sln is included with the distribution use that otherwise you may well have to create your own (or add it as a project to an existing solution).
Once you have a .lib add the directory under the VC++ directories of the project settings and add the actual .lib file name under Linker->Input on the Additional Dependencies line.
To build the library if the distribution does not include the needed VS files you will need to create a project at minimum (it can be part of the solution for your program), right click on the solution node in solution explorer and select Add->New Project, from there select Visual C++->Windows Desktop and either Dynamic Link Library or Static Library as you desire.
Go to the Project menu and select Project Dependencies, change your program to depend on the new project, this will set build order so your program project builds after the library.
You may need to disable the use of pre-compiled headers, right click on the new project node select Properties, go to C/C++->precompiled headers and change Precompiled Headers->Precompiled Header to Not using precompiled headers.
Next add the header and source files to the project and attempt building.
If this succeeded you will have a .lib suitable for use in the additional dependencies of your program project as already described.

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.

Issues with including headers from static library

I may be just missing something, but I'm trying to include a MongoDB C++ driver library build into my DLL project. I'm trying to follow this guide
I've tried including the folder in Configuration Properties->Linker->General->Additional Library Directories, and the .lib file in Linker->Input->Additional Dependencies. I've also added the /MT command-line option.
Now here comes my problem - how do I use the files? Do I just use #include "mongo/client/dbclient.h", because this doesn't seem to work (Cannot open source file). I can't find much of any helpful documentation on this subject.
Any ideas?

Visual c++ 2008 express creating a library

I know many have asked this question, but following the things suggested hasn't seemed to work for me. So I thought I'd ask for help, as I'm obviously missing something.
I've spent the past couple days writing some code that uses the boost library that allows me to open text files, translate the text into a usable data format, and save the data as a text file when I'm done with it. I built it specifically to be extendable to deal with different datatypes (specifically any object or class I make down the line) so that I could link to it as a library in my project as I do other libraries.
However, even after compiling it into a lib and adding the directory of the lib to the "Additional Include Directories", I get an unresolved external error whenever I try to include one of the header files in the library. I've also tried putting the direct path to it under Additional Dependencies (as well as just the library name with the path to the directory set in Additional Include Directories and Additional Library Directories. I also tried it with only one of those on at a time and it still failed).
Is there something I'm missing? How can I compile this code I'd like to reuse and link to it in new projects without copying the cpp and h files into my solution every time? Would a DLL work better/easier? If so, how would I go about compiling it as a dll and linking to it?
Thanks for reading this wall of text. Any help you could provide would be great.
Ok. While it's not the exact solution I was going for, I managed to figure this out. For anyone running into this problem, there's a wonderful tutorial that explains how to make DLL files and make use of them (at least on windows, don't have any computers using an alternate OS set up yet to test the DLL on). You can find it here.
http://programmingexamples.wikidot.com/blog:1
The gist of the problem is you need to also keep a copy of the .h file handy to include in your project. (what I did is created a directory on my C drive that holds the libraries I'm making, and in that I have a folder for the .lib files and a folder for the .h files.) Either import the .h file directly into your project directory or add it to your project properties (C++ -> General -> Additional Include Directories ). Make sure for that, you're linking to the directory, not the file itself. In the linker, you need to add your .lib file to the Additional Dependencies section (Linker -> Input -> Additional Dependencies). You can put the whole path here, or you can just put the file name, then tell the program where to find the directory it's in (add the path to the directory to Linker -> General -> Additional Library Directories)
Hope this helps anyone else who managed to miss something that everyone else seems to understand instinctually :P

fatal error LNK1104: cannot open file 'libboost_system-vc90-mt-gd-1_43.lib'

Made a new project, added main.cpp and wrote the code at this URL:
http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp
Also, added the appropriate include path.
What's next?!?!! It seems like a darn mystery to build a boost code!
Been digging on it for more than 10 hours.
Can anyone give a straightforward answer on how to build the boost library from the code under windows, VC9?
You need to use bjam. It is responsible for creating the libraries that your application will use. Once you use bjam, you are going to need to instruct your project to include the lib file. You do this by going into the project's properties -> Configuration Properties -> Linker -> General. Add the directory which created the lib file to Additional Library Directories. You should be capable of correctly linking after that.
I believe that this link describes the steps better than I could attempt in a post
#bogertron: I want to hug you! (:
So, at last, with a real head ache, I've managed to do so.
The exact steps I took, so others will be saved from the head ache:
Went to http://www.boost.org/doc/libs/1_43_0/more/getting_started/windows.html#or-build-binaries-from-source .
Followed the "5.3.1 Get bjam" clause. Downloaded the "pre-built bjam executable for your platform" at this URL: http://sourceforge.net/project/showfiles.php?group_id=7586&package_id=72941 .
Extracted the ZIP file (my platform is Windows).
Ran "build.bat". It produced the file "bin.ntx86/bjam.exe".
Copied this file to my PATH ("...../boost_1_43_0/").
From a command line, executed "bjam.exe" and waited for about 4-5 minutes.
Then got this message at the console: "The Boost C++ Libraries were successfully built!"
That's it, it even tells you where the include & lib files are.
Not all Boost libraries are header only, Boost.System is one of them. The documentation for Boost.Asio tells you that it depends on Boost.System.
With Visual Studio, the Boost libraries use auto-linking, which means you don't have to explicitly link to the libraries you need. You however need to have the necessary libraries in the library search path, either globally or per-project.
If you don't want to build the Boost libraries yourself, you can use the installers provided by boostpro (only up to Boost 1.42 though at the moment). If you want to build them yourself instead, the documentation covers that well.
I had the same error and i followed the SourceForge. The link will take you to a folder of zipped lib and dll files for version of boost.You can download and unzip related library and when copied it to the related location everything gonna be alright ;)
In Visual Studio, right-click on your project, then go to properties and select:
Properties -> Configuration Properties -> Linker
There you add the full path of your library in the tab named "Additional Library Directories".
From the "Boost Getting Started Directions":
"5.2 Or, Simplified Build From Source
If you wish to build from source with Visual C++, you can use a simple build procedure described in this section. Open the command prompt and change your current directory to the Boost root directory. Then, type the following commands:
bootstrap
.\b2"
On Windows, I opened the Visual Studio Command Line from the start menu. First I types "bootstrap" and then I typed ".\b2".
Now with boost_1.50 the Boost.build is included in the zip file: check tools\build\v2\ or better read: ...boost_1_50_0/more/getting_started/windows.html