Shieldfoss
Shieldfoss

Reputation: 15

Can you create multiple header files for a DLL? Can you consolidate header files?

Background: I'm using Project Euler as an excuse to test various C++ features and find holes in my knowledge. I have a project that creates an executable which solves various math problems. It's getting unwieldy and I'm splitting the problems out into separate executables. A lot of the math functions are re-used in several of the problems.

First: I want to create a DLL file with math helper functions. Rather than provide one single mathhelp.h file for the entire dll, I'd prefer to have several separate .h files e.g. primes.h, cartesian.h etc./ that each provide access to separate namespaces in the dll.

Is this possible?

Second: Separately, I do want a single mathhelp.h file for those projects big enough they just need access to every helper function without including fifty separate headers. How to do this is answered in:

How to export multiple header files as a single header file in C++?

but that answer gives me a follow-up question: If, in an executable project, I include the following header from the DLL project:

#ifndef MATHHELP_H
#define MATHHELP_H
#include "primes.h"
#include "cartesian.h"
#endif

...how does that compile if I don't also have primes.h in the executable project? Won't the pre-compiler reach the first #include and choke on the fact that I don't have primes.h in the project?

Upvotes: 1

Views: 1520

Answers (1)

Alberto12
Alberto12

Reputation: 509

When you provide a c++ compiled API to a user ( let's say it is compiled in a .dll file) you should provide beside the .dll file also the .h files (containg all header that were used to compile that .dll) and .libfiles ( containg the exported symbols from that .dll ). Now let's say that your user has the executable a.exe, and wants to use your compiled API. Usually you will provide them a file structure which looks more or less like that :

include / primes.h
          cartesian.h
          mathhelp.h

lib     / Api.lib

bin     / Api.dll

When an external executable uses your code he will need to add external flags in the compilation procedure like : -I/pathToInclude -L/PathToLib -l/yourLibrary ( you need to check the exact sintax for doing that). In your code you you will have something like this:

#ifndef MATHHELP_H
#define MATHHELP_H
#include <primes.h>
#include <cartesian.h>
#endif

In this case the compiler will look firstly in the current directory ( where he won't find the headers) and then in the folders specified by the -I flag ( where he will find your headers).

Upvotes: 2

Related Questions