Hi there. I'm pretty new to the C++ language (came from Python :lol: ) and I was wondering how to create a library.

In Python I used to create files with functions that can be used in other programs, and I want to know how to do that in C++.

So my question is: How does one go about creating their own library for personal use?

Member Avatar for iamthwee

You mean like your own header files?

I guess so. Is a header file like <iostream> and <string>? If so, then yes!

Member Avatar for iamthwee

Yeah...

#include "myheaderFile.h"

Ah, ok.

Thanks a lot =]

If a library != a header file, what should I use for my problem?

Say I have function A. I write it to file A, and save it. Now I have file B, and I want to use function A from file A. Would I use a header file or a library? And how would I go about creating it?

First, perhaps revisit that link and choose a library type you want; simplified: static or dynamic. Share the platform and compiler chain with which you want to make this library for better advise about the tools to use.

A nutshell might be this: compile your functions you want in your library and have a linker/librarian create the library. Using the library with your other code will be similar to the use of the standard libraries, but with a bit more work.

[edit]I can probably better help with the static library option if you're using gcc.

In the simple case of a few functions, maybe class definition, and you don't mind it being compiled with every new program you use this collection, simply saving the file as a .h (or even a .cpp) file and and using #include "myfile.h" add it to the current project.
A "library" is separately compiled and connected to your program via the linking step.
So, choose the option that best suits you.

Yeah, my plan was to just have a few functions. The .h file method will do just fine then. Thanks for the help!

Don't get in the habit of putting code in a header.

Why not? Is it because soon I'll be putting more and more functions into 1 file, and using it as a header will slow it down?

No. (As a general rule when you're learning the language COMPLETELY ignore compile speed.)

It's just a bad habit. Take a look at the header files on your system: #defines, types, prototypes; conspicuously absent: code.

[edit]BTW and for other pedants, yes I know the exceptions and various caveats, I just don't care to get into it at the moment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.