13

I'm new to C++. I just downloaded the Boost libraries to study. I wanted to look into some implementation details, so I looked for .cpp files. To my surprise, I haven't found any so far.

There seem only .hpp files out there. Where are the .cpp files?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Terry Li
  • 16,870
  • 30
  • 89
  • 134

7 Answers7

19

From the Boost documentation:

Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking.

See that link for the list of libraries that are not header-only and must be built separately. For those libraries, the .cpp files are in the /libs directory of the Boost distribution. If you got the precompiled package, you'll instead find the already-compiled .lib files in the /lib directory.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
razlebe
  • 7,134
  • 6
  • 42
  • 57
  • 1
    Appologies, I somehow missed the part about the `/libs` directory. Edited mostly for the sake of changing -1 to +1. – Mooing Duck Nov 22 '11 at 17:03
9

The .hpp files are the headers that you must include in your code in order to use Boost classes. Many Boost libraries are header-only; all of the implementations are in the .hpp files. For those that do have source, you only see the compiled versions as .lib files.

If you download a source distribution of Boost, it should have several subdirectories:

  • boost: contains .hpp headers
  • lib: contains .lib files (compiled implementation details)
  • libs: the source of those implementation details
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Guerrero
  • 534
  • 4
  • 11
  • If "all of the implementations are in the hpp files" holds, your answer seems the only one that makes sense to me. It is also a very neat one. Why aren't people voting it up? – Terry Li Nov 22 '11 at 18:05
  • @razlebe I assumed people typically put interfaces in header and details in CPP. Interesting point though, thanks! – Terry Li Nov 23 '11 at 14:41
  • @TerryLiYifeng I think there may have been some confusion about my answer. Hopefully to clarify: the "all of the implementations are in the hpp files" was only for those libraries that are **header_only**. For the other libraries, the source (.cpp files) is in the libs folder, as said in [razlebe's answer](http://stackoverflow.com/q/8229663/27615). Feel free to switch answers if you feel it is appropriate. – Guerrero Nov 28 '11 at 20:58
6

Because many (but not all) of the libraries are implemented using templates, and must therefore be placed within header files.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
5

A lot of the Boost library are purely template. In the previous standard of C++ there was already the keyword export to allow the developer to separate the implementation from the interface.

The sad truth was that the keyword never worked completely (difficult to implement from the compiler vendor point of view and difficult to use it right for the developer). One way to fix the problem was provide interface and implementation in a header file and avoid the implementation file. By the way, there are several Boost libraries that you need to compile and link in order to use it, and I bet you will find implementation files in those libraries.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alessandro Teruzzi
  • 3,918
  • 1
  • 27
  • 41
  • This is also a very good answer to me, which solves my confusion why they provide interface and implementation together in header. – Terry Li Nov 22 '11 at 18:12
4

For those which aren't header-only, the source files can be found inside the libs sub directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ylisar
  • 4,293
  • 21
  • 27
3

I believe the majority of the Boost libraries are implemented in the actual header files only, as previous posters mentioned. As was also mentioned, compiled implementation code will be included as separate library files when separate from the header files.

You mentioned being new to C++, so I think it's worth mentioning that this type of library distribution is not particular to Boost. Other third party libraries and APIs you use will likely be structured in the same way; you will find packages of header files and library files only, with no .c, .cxx, .cpp, etc files. This is done for a number of reasons, including to hide the implementation of the library functionality, and to allow shared libraries to be loaded into memory once each.

This article might help clarify things for you:
http://www.learncpp.com/cpp-tutorial/a1-static-and-dynamic-libraries/

8bitcartridge
  • 1,629
  • 6
  • 25
  • 38
2

You are probably looking at precompiled package, where cpp files are available in shape of libraries, not source. If you actually grab the source you will find some cpp files.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173