Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


"#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.


Or 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.


In 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.


Yes having platform specific code in its own header source and just #ifdef the includes provides much cleaner and nicer code


Don'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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: