简体   繁体   中英

Include from different library

I have to create a factory method which can create objects from different libs. My directory tree looks like this:

libA/src/...
libB/src/...
libC/src/...
src/...

libA, libB, etc are compiled separated.

Let the factory class be in libA/src/Factory.h. It should create an object from libB/src/someclass.h.

If i include ../../libB/src/someclass.h in Factory.h, then libA cant compile, because it can't find includes from someclass.h (The comiler looks for them in libA/src, but those are in libB/src) If i don't include, it doesn't know Someclass, so i can't create an instance of it.

Is there any solution for this?

You're looking for forward declaration: in headers where the class only needs to know the type, declare it with class AFactory; instead of including the whole Factory.h.

See this tip for more info .

EDIT

Having re-read your question, I find a contradiction between the fact that you want libraries to be compiled separately, and that factory of libA must know class libB. You can't compile separately then, so I would simply provide all necessary directories paths to include path.

I tried to reproduce this error but my example with the same directory structure and it worked fine.

#ifndef FACTORY_H_
#define FACTORY_H_


#include "../../libB/src/Someclass.h"


class Factory {
public:
    Factory();
    virtual ~Factory();
};

#endif /* FACTORY_H_ */

How do you compile them separately when there is a direct dependency? Maybe you want to use a static linked library. Please explain your intension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM