2

I'm trying to create a Qt shared library that wraps a lower level C library, meaning that I don't want that C library's header file to be accessed by the calling code that links to the library.

I'm following the steps here, which seem to be straightforward. I've constructed a SUBDIRS project in QtCreator. The library project builds fine, all classes and C functions are marked with the macro that expands to Q_DECL_EXPORT. The library defines a some headers that I want to include in the app project. The problem here is that when I include one of those headers, the chain is followed down to the C library header that is included, and at which point the application project fails to build since it can't find that header.

Qt's documentation specifically points out this issue, but is kind of vague about how to solve it.

#include <footronics/device.h>

 class MyDevice {
 private:
     FOOTRONICS_DEVICE_HANDLE handle;
 };

When deploying the library, there should be no dependency to the internal headers footronics/device.h or ui_widget.h.

So, how can I avoid the headers that I'm including from the library, from implicitly including the headers from the C library that I'm wrapping?

4

1 回答 1

3

如果您只使用指向共享库类的指针或引用,则可以使用Forward Declarations

class FooTronicsDevice;

class MyDevice {
private:
    FooTronicsDevice* _device;
}

编译器不需要知道类的结构来定义指针(或引用)。

如果这不可能,您可以使用 Qt 文档中建议的Pointer to Implementation习惯用法。这基本上意味着您将实现与公共接口分开。

于 2013-09-09T06:38:42.047 回答