Question

I'm writing a dll that will export functions and classes. But for now I have a question about exporting functions.

If my function signature looks like:

__declspec(dllexport) int fn1(FILE *theFile);

Should I include so it defines FILE? What is the best practice here? And if I want to export a function that takes or returns a custom type, should the header file for those types be included in the exported header file as well?

Was it helpful?

Solution

Don't forward-decl standard library types and functions. That is what the library headers are provided for. Your file should be laid out as:

#ifndef MYLIBRARY_H
#define MYLIBRARY_H

#ifdef  MYLIBRARY_EXPORTS
#define MYLIBRARY_EXPORT    __declspec(dllexport)
#else
#define MYLIBRARY_EXPORT    __declspec(dllimport)
#endif

// required headers
#include <stdio.h>

// exported/imported functions
MYLIBRARY_EXPORT int fn1(FILE *theFile);

#endif

Your DLL project is built with MYLIBRARY_EXPORTS defined as part or the defined preprocessor macros (configurable any number of ways), and consumers of your DLL do not define it.

This is the common layout MS uses for their wizard canned DLL project generators. Consider doing the same in your projects as well. Above all, include the required standard headers for your library to properly define what it needs coming in, and what it provides going out.

OTHER TIPS

A header file usually should be self-contained. Clients consuming your header file should not have to know to that they need some #include "xyz.h" line before including your own header. (And imagine if that other xyz.h header did the same thing, and so on. You'd need to know a strict ordering for all header files, which would be a nightmare.)

However, you do not necessarily need to #include headers for some types that you use. For example, you might be able to use forward declarations instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top