Search code examples
c++static-linking

Including platform-specific libraries with one header file and separate source files


I'm writing a platform-independent sockets library. The basic file structure is like this:

source directory
|_________________________
|            |            |
v            v            v    
header.hpp   win32        linux
             |            |
             v            v
             source.cpp   source.cpp

The header file contains any #includes that are universal across operating systems. The source files #include platform specific libraries.

I've built many projects like this, but I'm running into an issue with sockets. Because I'm specifying default values for my function parameters, header.h needs the platform specific libraries for #defines used in the header file,like SOCK_STREAM, AF_INET, and SOMAXCONN.

Now, I don't want to use #ifdef _WIN32 statements in my header file, as I would like ALL platform specific code to be in the source files in separate folders.

Am I basically stuck between a rock and a hard place? Are avoiding #ifdef #includes and specifying default parameter values with header-dependent #defines mutually exclusive?


Solution

  • You could get your default parameters by retrieving them via functions that are declared in your platform independent header and appropriately defined in your platform specific .cpp files.

    Or, you could also declare them as extern consts in your platform independent header and appropriately define them in your platform specific .cpp.