Conditional Compile - Implementation Alternatives

173 Views Asked by At

I was looking for a good resource that provides a discussion on best practices for conditional compilation and cross-plattform portability for C projects.

For example:

Writing a network library that (depending on the underlying platform) either uses TCP or SCTP. The usual approach would probably be to define a common API specification and let the makefile decide to compile the correct implementation.

I cannot imagine that conditional compilation using MACROS and code duplication is a good idea here (since such a network implementation is mostly the same, with differing SEND and RECEIVE primitives).

Does anybody have good suggestions or a good resource discussing best practices to these problems?

Thanks for your input!

1

There are 1 best solutions below

1
On

Macros are good if you limit them to very few places. Example:

#if defined LINUX
#   include "linux/mylib.h"
#elif defined WINDOWS
#   include "windows/mylib.h"
#else
#   error "unsupported platform!"
#endif