3

I couldn't really find anything on Google or SO about this. I don't know if it's because it's uncommon, or because I am using the wrong terms to search. I read this question here, but it didn't really answer my question.

So what I am doing is trying to build a library on top of SDL for C++. Now what I can't seem to figure out is how to make projects that use my library be totally independent from SDLs files except the dll.

When I make my library, I link with SDL2.lib, and SDL2main.lib. I include all of SDLs header files into my libraries files. When I build, it generates my library file; GGL.lib.

But when I want to test my library in another project, I have to include all of SDLs header files, because in my project's Window.h, it includes SDL.h. I am wondering if there is any way for me to make my libraries header files independent from SDL except for the dll.

Community
  • 1
  • 1
Greg M
  • 954
  • 1
  • 8
  • 20
  • I don't know windows that well, but generally no, if you want to use a library you need its headers, even if it isn't a direct dependency. – Falmarri Nov 10 '15 at 21:26

1 Answers1

5

You obviously need to avoid including SDL header files in header files of your library. Include them only in source files, where possible. This removes redundant dependencies and speeds up compilation process. But you want complete independence, so if you can't remove one #include directive, you failed.

If you need to declare a pointer of an SDL type in your header (like one usually does with SDL), forward declare it. That type will however remain incomplete to the users of your library. In case your library is object-oriented, there's Pimpl Idiom, which is based on the same principle - this hides everything.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • When yoy say "avoid including SDL header files un hrader files of your library" you meen including them in the stdlib that comes with the compiler or you mean not including other headers on .h files but only on .c or .cpp files on a project? Honestly that is not too clear on this answer. – rlam12 Nov 23 '15 at 12:38
  • I mean the second. Directly or inirectly, the header files must not be included in your header files, only in `.cpp`/`.c` files. I was not aware `` included SDL, what do you mean? – LogicStuff Nov 23 '15 at 13:14
  • Was speaking in general terms, just wanted to know if your post meant that we should not include other header files in header files but include then on the .c/.cpp files. If that was the case then why it was a performance reason with compilation times. Sorry for the unclear first comment... – rlam12 Nov 23 '15 at 13:36
  • Yes, this can be applied for standard headers, too. But when you need a complete standard type in a header file, leave the include there, rather than filling your code with pointers. – LogicStuff Nov 23 '15 at 13:40