How to use C++ libraries? - c++

I am a Python developer and I have not used C++ since university. I am doing scientific programming with python, mainly. I wanted to try C++ to see if it is better performance-wise.
I am quite new in C++. I have found dlib library, which seemed a good library as it had many interesting features. But when I downloaded it, I found several folder full of .h and .cpp files.
In Python, I would have installed a wanted library using pip or something, then use it in my project using import.
Is there a similar installation for c++ libraries? Or do I have to look among all those .h and .cpp files and decide which ones I need in my project then copy them? Or how do I use the dlib library?
I have searched a lot on google but I could not find any indication on how to use a c++ library or install a new package to be used.
I use Visual Studio Community 2017 and Windows 10 if that matters.

To integrate a library, you need two kinds of things:
header files (usually *.h) with the declarations required to let the compiler know about the features in the library (a little similar to an import statement);
compiled library files (usually *.lib) containing the precompiled executable code itself, to let the linker assemble the final executable file.
In some cases (especially for templated code), a library can be made of header files only. In other cases, the package doesn't contain a ready-made library file and you have to build it yourself or manually include the source files (*.c/cpp) in your project.
Not speaking of the innumerable optional settings that you may have to adjust to comply with the specifics of the generated code, such as function calling convention, struct alignment...
Last but not least, some libraries are connected to at run-time only. They are called Dynamic Link Libraries and involve yet different procedures.
All of this is relatively complex and close to black magic for beginners. If you are very lucky, you will find some library documentation telling you which options to use for your compiler. If you can, start from an existing sample project that works!
For dlib, check http://dlib.net/compile.html.
Be ready for a cultural shock when you will compare to the ease of use of the Python modules.

It is quite a broad question, but I'll do my best.
First of all, in C++ libraries consist of header files and precompiled part (.lib,.dll on Windows, .a, .so on Linux). To use them in your project you have to inform your program (and compiler) about features that library has (by #including their header file) and linker to include that library binaries.
pip is package manager, which automatically downloads, builds and installs library that you want in your system. In C++ there is no such single tool at the moment and there steps must be done more or less manually.
For dowloading you usually end up with git or downloading the zip archive with source (do it here). Once you have sources you have to build it.
In order to achieve multiplatformity libraries usually does not get shipped with concrete build system description (Visual Studio Project on Windows or makefile on Linux etc.), but are created in more general tool CMake, which abstracts them. E.g. dlib does that. With use of CMake (For start I recommend CMake-GUI, which is installed with CMake on Windows) you can generate Visual Studio Project, which later you can open and compile to generate .lib file. How exactly to do it follow dlib compilation description.
Once you have you lib and headers files on your disk you can add headers and .lib to your Visual Project and use as any other C++ library. (Seems old, but should work)

As far as I know, there are no tools similar to pip for C++. What you have to do depends on your working environment and the respective library.
In case of dlib there are instructions on the project homepage for Visual Studio. Basically, it involves compiling the whole library alongside your own project by copying a single source file to it and setting up include pathes.
From http://dlib.net/compile.html:
Compiling on Windows Using Visual Studio 2015 or Newer
All you need to do is create an empty console project. Then add dlib/all/source.cpp to it and add the folder containing the dlib folder to the #include search path. Then you can compile any example program by adding it to your project.
Again, note that dlib will only be able to work with jpeg and png files if you link in libjpeg and libpng. In Visual Studio, the easiest way to do this is to add all the libjpeg, libpng, and zlib source files in the dlib/external folder into your project and also define the DLIB_PNG_SUPPORT and DLIB_JPEG_SUPPORT preprocessor directives. If you don't know how to configure Visual Studio then you should use CMake as shown above since it will take care of everything automatically.

You have to download them, put them in your project directory, and then include them almost the same way you would do in python. You need to include only the .h files.
Example for test.h:
#include "test.h"
Hope this helps!

Related

How to include SFML source code into my visual studio 2017 c++ project and compile

I have been programming a game in c++ using the sfml library. However, I would like to adjust some of the code of that library, and use that altered code in my project.
So instead of linking the dll I would like to add the source code and then play with that source code. (e.g. for speed optimization).
I know that doing something like that is generally speaking a bad idea. Howeover, I want to learn by playing around a bit and trying different things.
So how would I add the sfml source code to my c++ project in MS visual studio. Note that I am a total noob. I already tried adding the sfml folder that I downloaded from git in the project properties page called "Additional Include Directories", but i am getting errors, of the form "Cannot open include file: 'SFML/Graphics/GLCheck.hpp': No such file or directory" so I guess that i have not yet done enough.
If you want to modify the source code in the library, all you'd have to do is just navigate to where you have SFML installed and go into the code files with a text editor and edit them.
Then, you could link the library to your VS project the same way you would normally but that library is now modified by you.
Seeing as you have a search directory issue already in VS, you must fix that first. Fix that and then go and modify the library's .hpp, .h, .cpp, whatever files in-place.
To fix the search issue.... I don't use VS for graphics, I use CodeBlocks so I am not sure about their GUI to link libraries and change search directories... but, find out where you installed SFML. Check your /usr/include/, it's probably there. Specify that path in the search directories. Just go and find where that GLCheck.hpp file is located. For Example: Say it's full path is /usr/include/SFML/Graphics/GLCheck.hpp... then /usr/include/SFML/Graphics/ or just /usr/include/ (VS might handle it recursively) needs to be in the list of SEARCH DIRECTORIES.

Llinking ITK (Insight Toolkit) to C++ VS2008 solution without using CMake?

Question
Is there an easy, straight forward way of including the ITK libraries into my project (VS2008 solution) without using CMake?
Background
I have just started looking into ITK this week. So far I successfully compiled ITK & VTK and got the WikiExamples to compile (and run) as well. My next goal is to include any ITK functionality into my own project. I have no experiences with CMake except the use of it during the documented installation process.
My own project codebase is in itself a rather complicated, SDK environment in rather large, complex VS2008 solution to which I only want to add a bit of ITK functionality. As I have built ITK already, can I simply set includes to the .h files and add the lib directories to the linker?
Is there a good way to do this, as the .h files are spread over the whole ITK code tree?
I've realized that this question is similar, but the given answer was too cryptic for me, sorry.
Edit: After a 2nd and 3rd read, it actually wasn't. It led to my answer below.
When configuring ITK with CMake (or cmake-gui) one can set the variable CMAKE_INSTALL_PREFIX to a destination for all output. The default value is C:/Programm Files (x84)/itk
After Configure and Generate have been performed successfully, the solution file (ITK.sln) has been created in the given "Where to build the binaries" directory and can be opened in Visual Studi0 2008. The solution has the default project ALL_BUILD which builds the solution, but it also has a INSTALL project which is skipped by default.
Building this solution (after ALL_BUILD) copies all required lib, .h, .dll etc. files into the path specified before.
This folder structure is the place to link my own project up to.
Linking in your own project
In your own project of VS Studio (I'm on VS 2008) you have to modify your project properties to include the include files:
And you have to modify your project properties to include the library files for linking:

Linking problems with TinyXml++ and Visual C++ Express Edition 2008

I'm trying to use TinyXml++ in my project, and I have tested it successfully in a small program before. I recently decided to use it to read a configuration file in my current project and though the code compiled, I got a ton of linker errors (unresolved external symbols mostly). I read in some forum thread, describing a similar problem, that I had to use the same runtime library as TinyXml++ was compiled with. I opened the accompanying Visual C++ 2008 project and saw it was using Multi-threaded Debug (/MTd).
I have now tested all runtime libraries with and without #define TINYXML_USE_TICPP before I include "ticpp.h". I've gotten from 9 to 80 linker errors (which I why I initially didn't want to post them all, but I will if you want to see them).
I have copied the static library ticppd.lib (found in the download folder) to my project's directory and linked it under Linker->Input->Addtional Dependencies. I have included a directory path to the TinyXml++ source under C/C++->General->Addtional Include Directories. What am I doing wrong? :(
TinyXML is a library with the full source code available; you should not need to include a .lib. You could separate TinyXML into a separate project and mark your project as depending on it, which will allow you to compile both projects with consistent settings for threading and debugging, but the easiest way to get it working should be to just add the TinyXML .cpp and .h files (just not the test one) to your project and compile.

Using a DLL with unmanaged code in Visual Studio 2010?

I'm fairly new to C++ and an trying to figure out to use the TagLib library for a project I am working on. I'm working with unmanaged C++ in Visual Studio 2010 on Windows 7 64bit. I've never used an external library before so I'm very confused on how to go about this.
From this blog entry I got the libtaglib.a and taglib.dll files. I ran across this SO question on how to use TagLib, but it deals with QT Creator, not Visual Studio and I'm not knowledgeable enough about the subject to understand what is being said to translate it into what needs done for Visual Studio.
So, some questions:
Is it even possible to do this with unmanaged code?
What exactly is the function of a .a file?
Most importantly, how do I go about using the taglib.dll in my program??
I've been all over Google looking for a way to do this, but my major problem is that everything I run across is over my head. Please let me know if more info is required. Any help is very much appreciated! Thanks!
I seem to have gotten it working successfully. Here's a rough outline of what I did:
1.) I used CMake to generate the Visual Studio solution.
2.) I attempted to build the tag project in the VS solution, but it failed.
3.) I made the corrections to a few source files as outlined here: http://old.nabble.com/taglib-fails-to-compile-with-MS-VC%2B%2B-2010-td29185593.html
4.) I built the tag project again in release mode. This time it was successful.
5.) I copied the resulting dll, def, and lib files to the same directory as the source files for my project.
6.) I copied the header files from the taglib source to a subdirectory in my project (not sure if this entirely good practice)
7.) In my project settings, I set the subdirectory with the header files as an additional include directory.
8.) I added the dll, exp, and lib files to my project by just going to Add>Existing Item.
9.) I added some code from the taglib examples and built it. Everything worked so I think I got it.
One caveat I ran into, since the DLL was built in release mode, my project had to be run in release mode or it would crash. I'm guessing that if I replaced the DLL with one built in debug mode I could run my program in debug mode, but I have not tried this.
You cannot use libraries specific to GCC (you can tell because they have .a extensions) with Visual Studio. You will have to build the library from source in order to use it with MSVC. Once you have done that it's a simple matter of adding the .lib generated from the build process to your project and things should work out of the box. (Note that it's a .lib you need whether you're compiling for dynamic linking or not -- doesn't matter in msvc land)
EDIT -- after looking at TagLib itself --
In order to compile TagLib you'll need to get the CMake build system, and TagLib itself, and have CMake build you a visual studio solution. Using that solution you'll be able to build the .libs and .dlls you need. Note that because TagLib is a KDE library, you'll probably need to also build some QT bits in order for everything work work successfully. However, I don't have specific experience with the library so I'm not going to be all that helpful here.
Yo do not have to recompile the source (to create the .lib file) if you have the .dll file. With dumpbin /exports and lib (both came with Visual Studio) yo can create a lib that you can link with your application. In this link you can see a nice explanation: http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/
But as Billy Said, probably you would need other parts of QT to use this library.

Cross-platform svn management (Makefiles & Visual Studio)

I'm working on a little game called freegemas, it's an open source version of the classic Bejeweled written in C++ and using gosu as the graphic API. I've been developing it under Ubuntu Linux as usual, but the other day I wanted to give it a try and I compiled it on Windows using Visual Studio 2005 (which I had never used before). The program worked flawlessly.
To compile it on Windows I manually copied all the source and header files to a new project on MSVC, but I would like to adapt the SVN so I don't have to recreate the project every time I want to compile it.
Therefore, the question would be: What's the best way of organizing the svn so I can have, on the one hand, a Makefile to compile the project in Linux, and, on the other side, the MSVC project's files? Right now I've got a simple folder called trunk with all header, source and resource files on it.
I've never used Visual Studio before, so I don't know which files are the most important either. Maybe some of those files are auto-generated and do not need to be svn-versioned.
Thanks in advance.
You could just keep the project files in a seperate directory "winbuild" or similar. Still, to maintain them would require manual interaction (ie adding every new file manually). The only files you would need to upload to svn are the *.vcproj (for MSVC 2005/2008) and *.vcxproj (MSVC 2010).
Alternatively, you could opt for a cross-platform solution like CMake, which could generate makefiles and Visual Studio project files from a common CMakeLists.txt, which is the only "project file" that would have to be maintained (instead of your makefile). Especially for a simple (?) project like yours (some headers+sources). There would be no need to include any makefiles or vcproj files at all, just the CMakelists.txt file would suffice.
There are others like CMake (SCons, boost.jam, jam, premake, etc.)
It should be feasable, but requires some testing and trial-and-error.