We help IT Professionals succeed at work.

gnu c++ header files for libraries.

sean-keys
sean-keys asked
on
Hi I created a qt based library which works fine.  My issue is that I'm trying to figure out how to cleanly expose my header file without creating an inclusion mess.

For example the app that calls my library needs only one header file.  My problem is that header file has other inclusions that don’t relate to the functions it provides.

SerialIO.h

#ifndef SERIALIO_H_
#define SERIALIO_H_

#include <stdio.h> //for printf
#include <fcntl.h>
#include <string.h> // for memset
#include <string>
#include <inc/AsyncRead.h>
#include <inc/AsyncWrite.h>
#include <inc/types.h>
//#include <inc/CircularBuffer.h>
#include <QObject>
#include <QString>
#include <QWaitCondition>

Open in new window


The lines:
#include <inc/AsyncRead.h>
#include <inc/AsyncWrite.h>

are only needed to compile the lib itself not link against it.

What is the standard/proper way to accomplish this?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
Basically there'd be two ways to addres this - either by conditional compilation or bey creating a separate header that only contains the declarations necessary to use your library. E.g.

SerialIO.h

#ifndef SERIALIO_H_
#define SERIALIO_H_

#include <stdio.h> //for printf
#include <fcntl.h>
#include <string.h> // for memset
#include <string>
#ifdef BUILDING_LIBRARY
#include <inc/AsyncRead.h>
#include <inc/AsyncWrite.h>
#endif
#include <inc/types.h>
//#include <inc/CircularBuffer.h>
#include <QObject>
#include <QString>
#include <QWaitCondition>
                                  

Open in new window


or

SerialIO.h

#ifndef SERIALIO_H_
#define SERIALIO_H_

//#include <stdio.h> //for printf - is that used in the header? If no, remove it
//#include <fcntl.h> // anything used in the header? If no, remove it
//#include <string.h> // for memset - is that used in the header? If no, remove it
#include <string> // any functions declared that return or take types from this one? Keep it
#include <QObject> // any functions declared that return or take types from this one? Keep it
#include <QString> // any functions declared that return or take types from this one? Keep it
#include <QWaitCondition> // any functions declared that return or take types from this one? Keep it
                                  

Open in new window

Author

Commented:
I think I would prefer the conditional method. Let me give it a try.  Thx!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.