0

First of all, I use c++11 on linux with g++4.7 for all parts of my problem.

The setup of my problem: I created a shared library (lets call it "libA") that I use in different programs. Within this library is a part that was not exposed in the interface since it's not relevant for the programs. Now, however, I'd like to use this previously hidden part in another library ("libB") directly.

My plan was therefore to create a new library from the hidden part of libA. This would then be "libSub". libsub is then included into both libA and libB. Both compile without errors. But when I now try to compile a program that depends on libA, I get lots of errors saying that there are undefined references to functions from libSub.

In order to make the structure a bit clearer:

// Sub.hpp
class Sub{
    private:
        // private variables
    public:
        // interface functions
};

// A.hpp
class Sub; //forward declaring the sub-class
class A{
    private:
       std::shared_ptr<Sub> s;
       // more private variables
    public:
        // some interface functions
};

// A.cpp
#include <Sub.hpp> // include the declaration of the Sub class
// definitions of the member functions of A

// program.cpp
#include A.hpp
a=A();

The libraries are placed in local folders since I wanted to avoid installing them to the general lib-folders. I guess that installing them all to the global lib-folder would solve the problem.

And the question is: Is there a way to get rid of the errors and still use local folders? And if so, how?

1 Answers1

0

Have you tried to compile only this alone?

// A.hpp
class Sub; //forward declaring the sub-class
class A{
    private:
       Sub s;
       // more private variables
    public:
        // some interface functions
};

Here Sub is an incomplete type and A will not compile because you need to have the class defined. Look at this to see how you can use forward declarations.

I assume that you need to start by including the header files of your libSub in your other libraries. Do that with a small example and come back if you have more problems.

Community
  • 1
  • 1
FKaria
  • 1,012
  • 13
  • 14
  • OK, I read the post regarding forward declarations and you are right, the files as I posted them would not compile without errors. I assumed that it would not make a difference whether I have a Sub-type of a std::shared_ptr type as member variable and therefore I tried to simplify the concept. In the real files, however, I actually use a shared pointer to a Sub-instance. Therefore, the files compile without errors. – PirateDougTheBlack Nov 07 '13 at 16:18
  • I'd also like to avoid to tell all programs about the newly created libSub. I tought that this is the beauty of shared libraries - you don't have to recompile a program as long as the interface of the library stays the same. And since I never use a function of the Sub-class directly in the code of the programs I thought this should be possible. – PirateDougTheBlack Nov 07 '13 at 16:23