How to avoid #include dependency to external library

前端 未结 2 1887
野趣味
野趣味 2021-02-02 13:39

If I\'m creating a static library with a header file such as this:

// Myfile.h

#include \"SomeHeaderFile.h\" // External library

Class MyClass
{

// My code

}         


        
2条回答
  •  梦谈多话
    2021-02-02 14:35

    Say the external header file contains the following:

    external.h

    class foo
    {
    public:
       foo();
    };
    

    And in your library you use foo:

    myheader.h:

    #include "external.h"
    
    class bar
    {
    ...
    private:
       foo* _x;
    };
    

    To get your code to compile, all you have to do is to forward declare the foo class (after that you can remove the include):

    class foo;
    
    class bar
    {
    ...
    private:
       foo* _x;
    };
    

    You would then have to include external.h in your source file.

提交回复
热议问题