4

How might I write a single header file that defines an interface and use a separate source files to write platform-specific code?

For example:

video.h
video_windows.c
video_linux.c
video_osx.c
Mogsdad
  • 44,709
  • 21
  • 151
  • 275

1 Answers1

8

In your question you have all header files while you are talking about a shared header between source files.

In any case you just provide a common .h file and have 3 different

video_windows.c
video_linux.c
video_osx.c

You then include to your makefile (or whatever you use) the correct one according to the platform.

If you want to separate code in header files or in source files directly you can easily use some predefined macros, see here.

Community
  • 1
  • 1
Jack
  • 131,802
  • 30
  • 241
  • 343
  • Are you saying that the header file only has to match the object file in name? i.e. a.h matches a.o? –  Feb 16 '12 at 05:25
  • 3
    The name of the header is not related to the name of the source file. You can call it `foo.h` and then define its methods in a file called `bar.c` without any kind of problem. You just `#include "foo.h"` in source file and in all the others involved.. there are no restrictions or constraints to keep with header file names. – Jack Feb 16 '12 at 05:30