2

This question has been for quite a long time for me. I don't use c++ very often, and it seems most cpp libraries are provided as a zip file or tar ball containing source code.

So, the question is:

Why those projects don't provide a pre-compiled binary file?

My current understanding of the "autoconf, make, make install, ..." procedure is to make sure everything is correct and then compile source codes to a executable file or some libraries. But if we are eventually getting the same thing, why should we compile it every time? I didn't mean to maintain a separate project for compiled libs, but during debug and test process, those files should be generated automatically, why don't just put them into the release file?

For example: the FTGL library's source comes with visual studio solution files (.sln), if they uses this to develop and debug, why don't they put the compiled binary into their release file?

4
  • 8
    Because different operating systems and distributions need different binaries.
    – Barmar
    Commented Jun 11, 2020 at 16:06
  • Hmm, could you be more specific? Binaries for which exactly platform, what architecture, ABI, optimization level? Vector instructions on or off?
    – bipll
    Commented Jun 11, 2020 at 16:08
  • 2
    Also, different compilers use different library formats, object file formats, debug info formats, etc Commented Jun 11, 2020 at 16:10
  • These days I prefer not to download binaries but to have the code support building using CMake to generate my Visual Studio (or other IDE) project files.
    – drescherjm
    Commented Jun 11, 2020 at 16:18

2 Answers 2

7

Platforms generally have a common C ABI that dictate symbol naming, function calling conventions, and other low-level details. Compilers that target that ABI can interoperate with other compilers that do the same. Linux libraries and Windows libraries are incompatible, but it is usually possible to build a small handful of versions for each targeted platform: one for Linux, one for Windows, etc.

There's no such standard for C++ libraries. Name mangling varies from compiler to compiler. Compiled C++ libraries can typically only be used by the same compiler at the same version. Libraries compiled by different versions of the same compiler aren't even necessarily compatible. To pre-build a library you'd have to build with dozens of versions of g++, dozens of versions of clang, etc... It's not practical.

Distributing C++ libraries is difficult. Some common strategies are:

  1. Make the library header-only so end users just #include library headers. No compilation required.

  2. Distribute header and source files and require the end user to build the library.

  3. Implement the library in C and provide C++ wrappers in header files. The library can be used by both C and C++ programs. The C portion can be pre-built if so desired.

  4. Inverse of the previous bullet, implement the library in C++ and add C wrappers. The wrappers would define a bunch of freestanding extern "C" functions that call into the C++ code.

Boost, for example, uses a mix of strategies 1 and 2. If you only use the parts of Boost that are header-only then all you have to do is #include their headers in your program, super easy. If you use non-header-only pieces such as the threading or regex components then you have to compile them, which takes a while.

3 and 4 are useful for proprietary libraries where you don't want to distribute the code.

2

Depending on your platform the generated binary will be quite different. For example you may run Linux, Mac or Windows and beyond that these could be 64 bit or 32 bit architectures.

If I passed a windows 32 bit binary on to you and your systems was a 64 bit Mac the binary would be completely incompatible.

Some projects do provide a binary for each platform or maybe a select few however it's still great to have the source code as you may wish to modify it, only use certain parts of it or simply understand it.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.