Adding library code to your project, or use precompiled binaries? - c++

I need some advice on my project. I am going to use various C++ libraries to accomplish different tasks. I am using Visual Studio 2008. To me, it seems to get a little out of hand when I add the actual source code of the library to my project's path.
It seems easier to just use the include files of the library, and just link precompiled binaries to my application. So my question is this. Is it better for me to include the source code of each library to my project, compile and link, or will it be better to just compile the libraries separately (or download a precompiled version) and link it to my program? Are there any pitfalls of the second way?
Thanks

If a library is available use it.
With C++ and windows you may have to rebuild the library if it was built with a different compiler, there are issues linking C++ libs between gcc/mingw and visual studio.
but in general there is no reason to add the source to your code base.

Depending on the particular library, you might want to be able to debug it. Just downloading precompiled libraries and no source might make that difficult.

Related

What is the proper way to include a source library in a Visual Studio C++ project?

Right now I'm trying to create my first "real" project in C++ using Visual Studio 2019. I need to include a third-party library as a dependency. The instructions on the project homepage simply recommend to include all source/header files directly into the project. My understanding is that it's a bad practice, since the end result would look quite ugly in any VCS.
Thankfully, the library author also provided build scripts that call cl, lib and link with appropriate arguments and produce the coveted foo.lib. I added that file to dependencies in linker options and after some haranguing with compiler/linker options finally got it running.
To my distress, I realised that I've done all those manipulations in Release configuration, which prevented me from using the debugger. I then built the library with /MDd, fixed some compiler options... and got a bizarre compile-time error in vcruntime.h ( fatal error C1189: #error: _HAS_CXX17 and _HAS_CXX20 must both be defined, and _HAS_CXX20 must imply _HAS_CXX17).
At this point, I knew I was doing something terribly wrong, since including a simple library should't require so much manual knob-tweaking. What is the right, canonical way of including third-party dependencies in Visual Studio (and C++ in general)? Assuming the dependency isn't available on Nuget, vcpkg or somesuch.
As I understand from the stuff you did was on Windows. First of all I would recommend you try a linux distro. In windows it is possible to get lib files running, but not easy. It would help if you could send me a link to the library you are using.
The usual approach is to just stick those sources in their own directory and their own Visual Studio project (not solution). This can still build a foo.lib. You won't need many options for this.
Next, you just tell Visual Studio that your own project depends on that other project, and it will then link foo.LIB for you.
Having said that, it sounds like you try to build your two projects with different settings for the C++ version. VS2019 has good support for C++17 and experimental support for C++20, so you can choose. But you need to choose consistently for all your projects.
In larger solutions you can us a .vsprops file for that, which is like an #include for project files. A bit overkill when you have two projects, a lifesaver when you have 150.
It varies a bit how you include 3rd party libraries, sometimes 3rd party libraries have an installation and install themselves like under Common Components sometimes you have to do it manually.
E.g. YourSolution/3rdParty/foo/include
YourSolution/3rdParty/foo/lib
YourSolution/3rdParty/foo/lib/release
YourSolution/3rdParty/foo/lib/debug
Sometimes the libraries have different names then they may be in the same folder.
Once you have that structure go to your project's properties C/C++ and add the include under Additional Include Directories make sure you have configuration "All Configurations" here. Then go to Project properties/Linker/Input and the dependency for Debug Configuration and for the Release Configuration - since usually they are different libraries per configuration. e.g. release/foo.lib and debug/foo.lib (foo.lib foo-d.lib or whatever they are called).
Use the macros to make sure you get the right locations so that they are relative to your solution and project. It is not good having absolute paths. E.g. $(SolutionDir)3rdparty\foo\include
Disclaimer : I am not sure this is the "optimal" way to do it but that is the way I do it.

Is it possible to build Eigen lib for C++ instead of using pre-built files?

I am using pre-built 3.2.5 Eigen lib files, downloaded from website:
http://eigen.tuxfamily.org/index.php?title=Main_Page
I heard if I built the files by myself on my PC I could achieve higher compatibility with my processor what would lead to slight increase of lib's performance. Currently I am struggling with eigensolver calculation time being too long.
I use Visual Studio 2005 and I just add Eigen files location to my projects properties linker.
Is there any way to build those files myself on my platform? I am a bit confused how could I do it. Is it related to CMake?
There is no library to build, as Eigen is a "pure template header library". From the main site:
Requirements
Eigen doesn't have any dependencies other than the C++ standard
library.
We use the CMake build system, but only to build the documentation and
unit-tests, and to automate installation. If you just want to use
Eigen, you can use the header files right away. There is no binary
library to link to, and no configured header file. Eigen is a pure
template library defined in the headers.
You don't need to add the files location to the linker, but to the (additional) included directories in your project or to a property sheet.
Regarding calculation time, make sure you're running in Release and not Debug. There is a difference of about 100 in the speed. Also, make sure that optimizations are turned on (/O2 or /Ox).

How does a vendor supplied C++ library support multiple compilers?

I make use of a vendor supplied library in a C++ program. All the vendor supplies is a .lib, .dll and the header files.
This library was first used in MSVC++ 6 and now in MSVC 2010.
It appears they support mingw, qt, etc. I have had no problems with using it in either debug or release mode.
I have recently added another dependency to the project, yaml-cpp, an open-source project that provides no binaries. I am able to build yaml-cpp using cmake and MSVC 2010 as a static library and can link it to my project successfully.
In order to do this I must link to the debug and release versions of the .lib respectively as I have run in to weird Microsoft CRT/linker issues I am just learning about.
I am curious, how does the vendor of my first library (libXl if you are curious) provide a .lib and .dll that works on a range of compilers without worrying about the CRT version?
How do you write code that allows this or is it the linking process? yaml-cpp is a cross platform library, is it possible for me to compile it in a similar way that would allow me to use a single .lib within my project?
I am not sure what resources to look for to learn more about it. How do people learn about this kind of stuff? Thanks!
The reason that a .lib and dll can work with a variety of compilers is down to ensuring that there are no compiler-specific dependencies in your code. .lib and .dll are not recompiled so once they are made they should be stand-alone if so coded, and their functionality can be accessed from anywhere with the ability to use a .dll or a .lib.
There is some discussion on the pros and cons of dll and lib here which you may find interesting.
Let me know if you need more info and I will do my best to oblige.

Boost C++ libraries installation

I have just downloaded the boost libraries from the boost website and extracted them to my desktop. I was hoping to just have a quick look at them and have them installed on my machine and perhaps use them in the future when I am more accustomed to C++.
When I extracted it, I was confused with all of the extracted files. There is all of the headers in the boost directory but tutorials mention running bootstrap.bat (I'm using Windows).
So I am asking this: do I simply extract the headers to my compilers include directory like normal to get boost up and running or do I need to do something else?
As I understand it from searching about, apparently "most" of boost is just templates and can be used simply by including the headers, but what about the rest?
Am I totally barking up the wrong tree?
Thanks for any help
Since you mentioned you run Windows, take a look at this automated installer:
► http://www.boostpro.com/download/
Also, some general advice:
do I simply extract the headers to my compilers include directory
No! Do not pollute your compiler's includes with third-party includes; make a separate directory specifically for a particular library. You'll then need to tell your specific IDE in what directory it can find the library headers.
I usually use boostpro's installer, it is less work. I vaguely remember having to set up the BOOST_ROOT environment variable on one of my systems to use it.
The libraries that contained compiled source should be included in the installer.
If you don't use the installer (or don't set up your build correctly), and try to use the libraries that need it you will likely get some linker errors when you try and compile your program. Usually if you take those linker errors and plop them in google it tells you pretty quick which libraries you need to include in your build system. I use CMake for that and have been very happy..
Just add the root boost directory to include paths of your compiler/IDE (so if you have Boost extracted to C:\Boost, the compiler will use that path, not C:\Boost\boost).
Don't do any copying of the boost folder to your compiler's include directory, because it may make upgrading Boost harder in the future.
Also if you plan to use any of boost's compiled libraries, add Boost's lib directory to compiler's library search paths. Configuring the compiling step is a matter of putting the right toolset parameter to boost's build tool. If you have your command line environment configured properly, bootstrap should run and compile the builder without any problems, and the Boost builder should properly detect your toolset, so no parameters will be necessary.
As you do such configuration only once every time you do a clean install of your favorite compiler, it's not as hard or daunting as it seems.

Move C++ app with Boost from Linux to Windows with Visual Studio 6

I made a small program with Boost in Linux 2 yrs ago. Now I want to make it work in Windows. I found there are few .a files in my libs folder. I am wondering how to make it works in Windows? do I need to build Boost in Windows to get library or I can download somewhere? I am using Visual Studio 6.
Yes, you'll need to recompile for different platforms. Coincidentally, I posted instructions on this not long ago.
I hugely recommend you do not use Visual Studio 6. It's very dated, and terribly non-conforming. You can get the newer versions for free, as Express. You won't be missing anything.
Many boost libraries are header-only, you don't need to link against anything to use them. Libraries such as boost::filesystem require you to build libs appropriate for your platform and link against them.
Precompiled boost for MSVC7,8,9 can be found here (in the hope that you follow GMan's advice and get rid of VS6 …)
.a files from Unix are like .lib files in Windows. They will not work, and there is no way of "converting" them, short of using a compiler on the original source code.
However, Boost does build on Windows. Just download it (or more likely, the closest version you can find to the one your code was using).
Older versions of Boost did work under VS6, but with a whole lot of stuff disabled (VS6 really just barely qualified as a C++ compiler). If you can, I highly suggest you use a newer version of Visual Studio.
You have a few options. Building Boost will give you the greatest flexibility in working with your application and boost libraries. However, you can download pre-built Boost libraries wrapped up in a nice windows installer here, Boost Packages
But one thing to keep in mind, depending on what you used in Boost, a lot of it is template based so no libraries are required. If you're only using some templatized portions, you don't need to worry about any of this. Just make sure you have boost in your include path when building.
As a side note, you can get free but limited versions of newer MS compilers here: http ://www.microsoft.com/exPress/