What is the recommended method for creating a library with multiple classes?
Question: I am relatively new to C++ (previous experience in Python and dabbled in Java)
and I am writing a small program as a familiarization project. As part of the
program I am writing a class to decode some data and will eventually write a
similar one that will perform the encoding. The code is something that I am
sure I will reuse quite often and thought it would be interesting to create a
library as part of the project. My question is, what is considered the best
practice for creating a library?

**Edit: (Revised)**

After asking this question, I realized that I didn't know what I didn't know.
I have done some more research and this should help make my question more
specific:

  * I am developing in Qt Creator. So specifics relating to Qt would be helpful, but not necessary.
  * I have created a new static library project in Qt (MyCodec) that, currently, has one class defined called MyDecoder.
  * As a library, my assumption is that, to add MyEncoder, I just create another class/header file.
  * What happens next is where I am uncertain. Do I just build the library? My understanding is that it will create (in Windows) a .lib and a .h file. Is there something I should do before this step? Are there options that will affect the way I interact with it?
  * Do I just include that header file in my program to access both classes that I wrote?
  * I found lots of answers on adding a .lib file to a project in Qt, so I don't need that information.

**Original Question: (for context)**

My initial thought is that it would be the most convenient to create MyLib
that includes both MyEncoder and MyDecoder classes.

  * If I were to do it that way, do I just declare both classes in the header?

  * I would like to create a DLL out of this library for portability and experience. I'm sure there is lots of information out there about creating and using DLLs (which is not the subject of this question), but if there is a particularly good tutorial (for Qt) please pass it along.

  * My assumption is that it would be best to use separate namespaces for MyEncoder and MyDecoder for this implementation vs. one namespace for MyLib?

I can see one trade off of this method being the size of the application,
since including MyLib.h would include the code for the encoder and decoder (if
the encoder and decoder were separate applications). This is assuming I am not
using a DLL.

I guess what I am getting at is:

  * What methods are available (and recommended)?
  * What are the trade offs of each?
  * Where can I find documentation (tutorials/examples) on this specific subject? My search efforts have not yielded much for results.

If it helps to be more specific, I am doing my development using Qt 4.7.4 in
Qt Creator.


Answer: One "best practice" in C++ with regards to libraries is typically "you pay for
what you use."

How this applies to your question is that you would have MyEncoder and
MyDecoder in separate header files. So if the user wants to use a MyEncoder he
would include MyEncoder.h, if he wanted to use MyDecoder he would include
MyDecoder.h, and if he wanted to use both he would include both headers.

The linker will typically only include the parts of the code that you use in
the executable, so there is no penalty as far as code size goes, but there is
a penalty in compile times, particularly if you start using advanced template
techniques in your classes. Compile times can get pretty long in large
projects, so it is important to be able to only include what you are going to
use.

Of course, sometimes it is also convenient to include everything with one
header. So what you could have is this:

  * MyEncoder.h
  * MyDecoder.h
  * MyCodec.h

and then MyCodec.h could include both MyEncoder.h and MyDecoder.h

There is probably no good reason to have MyEncoder and MyDecoder in different
namespaces, assuming they are meant to operate on the same type of data.

You might want to have something like a MyCodec namespace, and declare
MyEncoder and MyDecoder within that namespace.

**Updated for your revision:**

> As a library, my assumption is that, to add MyEncoder, I just create another
> class/header file.

That is a correct assumption.

> What happens next is where I am uncertain. Do I just build the library? My
> understanding is that it will create (in Windows) a .lib and a .h file. Is
> there something I should do before this step? Are there options that will
> affect the way I interact with it?

I haven't used Qt creator in a while, so I can't speak with authority on it or
how to access the relevant options. But as a general rule you will want to
have at least 2 versions of your library; a debug version and a release
version. If your library uses the Qt libraries, then when an application links
to the debug version of your library, they will need to have the debug version
of the Qt shared libraries in their path, and if they link to your release
version they will need to have the release version of the Qt libs.

There may also be options of whether you want to statically link to the C++
standard runtime libraries, or dynamically link to the DLLs.

But essentially yes, you just build the library and then the application that
uses it will link the library to the executable.

> Do I just include that header file in my program to access both classes that
> I wrote?

You include the header file, and link to the .lib file. That's all you should
need to do.

