开发者

C++: how to build my own utility library?

I am starting to be proficient enough with C++ so that I can write my own C++ based scripts (to replace bash and PHP scripts I used to write before).

I find that I am starting to have a very small collection of utility functions and sub-routines that I'd like to use in several, otherwise unrelated C++ scripts.

I know I am not supposed to reinvent the wheel and that I could use external libraries for some of the utilities I'm creating for m开发者_Python百科yself. However, it's fun to create my own utility functions, they are perfectly tailored to the job I have in mind, and it's for me a large part of the learning process. I'll see about using more polished external libraries when I am proficient enough to work on more serious, long term projects.

So, the question is: how do I manage my personal utility library in a way that the functions can be easily included in my various scripts?

I am using linux/Kubuntu, vim, g++, etc. and mostly coding CLI scripts.

Don't assume too much in terms of experience! ;) Links to tutorials or places where relevant topics are properly documented are welcome.


"Shared objects for the object disoriented!"

"Dissecting shared libraries"


Just stick your hpp and cpp files in seperate directories somewhere. That way, it's easy to add the directory containing the C++ files to any new project, and easy to add the headers to the include path.

If you find compile time starts to suffer, then you might want to consider putting these files in a static library.


If you are compiling by hand you will want to create a makefile to remove the tedium of compiling your libraries. This tutorial helped me when I was learning to do what you are doing, and it has additional links on the site for more detailed tutorials on the makefile.


Unless it's very large, you should probably just keep your utility library in a .h file (for the declarations) and a .cpp file (for the implementation).

Just copy both files into your project folders and use #include "MyLibrary.h", or set the appropriate directory settings so you can use #include <MyLibrary.h> without copying the files each time you want to use them.

If the library gains substantial size, you might consider looking into static libraries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜