How to enable third-party open source in C ++?

I use several third-party libraries, such as boost, I have one class in my project, let it be called MyClass

All public functions of "MyClass" use only standard types (int, char, string), but private functions use smart pointers from boost and other algorithms from other libraries.

So, before I write the declaration of the class (and its functions) in the H file, I will write some included files.

To compile the project, add some additional include libraries to the project properties. And everything is working fine.

The problem is that when I want to use this class from another USERPROJECT project (extern class), I need to include the MyClass.h file in the USERPROJECT project, and then nothing compiles, because MyClass.h includes raising and other things, which are not configured in USERPROJECT (I did not configure additional include libraries here, and I do not want, because he should not know them, they are in private functions of the MyClass class).

What's the solution?

  • Should I break MyClass into 2 classes, one for the interface and one for the implementation?
  • Should I remove all incoming from H and MyClass and use forward declaration? (I tried, but could not compile it)
  • there is a better solution

Thank you in advance

+4
source share
2 answers

You can create compiler firewalls using the pimpl idioms:

// header file class C { public: ... private: struct Impl; boost::scoped_ptr<Impl> m; }; // cpp file struct C::Impl { // data member that were in C previously go here // }; 

Thus, code using your header file does not see the boldness of your class. This idiom is explained in detail here . However, you can still get binding errors if you use additional libraries that need to be linked. If you use only parts for the header only for promotion, then there should be no problem.

+2
source

Ideally, every external component is available in every project. (And everything is compiled with compatible options, etc.).

If you can do this, your problem will be solved. And the problems of other people around, who wanted to use the momentum, but faced the same obstacle.

If you cannot do this, you may have a solution using pimpl, but it adds complexity, maintenance costs, and to some extent reduces readability. And depending on what you use from boost, it may solve only part of the compilation of the problem, since additional lib may be required for linking. (if your thing is not self-sufficient, like a DLL)

In the latter case, if communication takes place on a client site, lib smuggling is required, but then it has the same amount of work to completely increase and avoid chaos. Therefore, do some research before action.

0
source

Source: https://habr.com/ru/post/1484658/


All Articles