Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include only BOOST smart pointer codes into a project?

What are best practices to include boost smart pointer library only without adding all boost libraries into the project?

I only want boost smart pointer library in my project and I don't want to check in/commit 200 MB source codes (boost 1.42.0) into my project repository just for that. What more, my windows mobile project itself doesn't even reach 10% of that size!

like image 589
Afriza N. Arief Avatar asked Mar 25 '10 03:03

Afriza N. Arief


People also ask

Why should you use smart pointers instead of regular pointers?

Smart pointers try to prevent memory leaks by making the resource deallocation automatic: when the pointer to an object (or the last in a series of pointers) is destroyed, for example because it goes out of scope, the pointed object is destroyed too. Save this answer.

Should I always use a smart pointer?

So, a smart pointer is only needed, when you use new or other means of dynamic memory allocation. In my opinion, you should prefer to allocate variables on the stack, so when refactoring code (to C++11), you should always ask yourself, if this new is needed, or could be replaced with an object on the stack.

Do you need destructors with smart pointers?

Boost smart pointers by themselves don't have anything to do with the need for a destructor. All they do is remove the need for you to call delete on the allocated memory that they are effectively managing.

How smart pointer is implemented in C++?

Smart pointer in C++, can be implemented as template class, which is overloaded with * and -> operator. auto_ptr, shared_ptr, unique_ptr and weak_ptr are the forms of smart pointer can be implemented by C++ libraries.


2 Answers

For just the smart pointer library, you have two options.

  1. Copy the headers you include in your source files (shared_ptr.hpp, etc.). Then copy over additional files until the project builds (make sure to maintain the directory structure).
  2. Use the boost bcp utility. For larger subsets, this tool saves a ton of time.

The former will make sure the fewest number of files possible gets added your project. The latter is much faster for any substantial subset of boost, but it will likely include many files you don't need (compatibility headers for platforms your program doesn't support).

like image 99
Sam Harwell Avatar answered Nov 20 '22 18:11

Sam Harwell


Just check in the folder containing the code you want? Try deleting/moving/renaming "everything else" and see what external dependencies the smart pointer library has, probably not many. I'm almost positive it doesn't require any built code (i.e. libraries), so just checking in all of the headers that get included seems like the way to go.

like image 41
dash-tom-bang Avatar answered Nov 20 '22 19:11

dash-tom-bang