3 ms·
Preprocessor magic is pretty much required if you want to write multiplatform code. I work on code for Solaris, Linux, and Windows, and makes life much easier
by cube13 15y ago
Preprocessor magic is pretty much required if you want to write multiplatform code. I work on code for Solaris, Linux, and Windows, and makes life much easier to use #defines to standardize the OS calls.
- dpark 15y ago"#define somefunc _someplatformfunc" isn't exactly preprocessor magic. That's just basic preprocessor functionality. "Magic" is the stuff that no one else looking at the code understands, and it's complicated and generally fragile and frankly unnecessary.
- ori_b 15y agoOr you write platform specific code in it's own files, and link it in conditionally. port-linux.c port-win32.c port-solaris.c myprog.c and just compile/link port-$TARGET.c into your program. The code tends to be cleaner, and the platform knowledge is concentrated in one place.
- Arelius 15y agoIn my experience this is a much better approach. Most modern compilers can inline across linker boundaries anyways, so it's not a real performance issue either.
- LefterisJP 15y agoYes having platform specific code in its own header source and just #ifdef the includes provides much cleaner and nicer code
- ori_b 15y agoDon't even do that -- have one header that all the platform interfaces implement without ifdefs. Keep the conditional compilation in the makefiles, not in the source.