[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

gnu c++ header files for libraries.

Posted on 2012-08-11
3
Medium Priority
?
671 Views
Last Modified: 2012-08-11
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?
0
Comment
Question by:sean-keys
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 38283991
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

0
 

Author Comment

by:sean-keys
ID: 38284118
I think I would prefer the conditional method. Let me give it a try.  Thx!
0
 
LVL 9

Expert Comment

by:user_n
ID: 38284121
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question