I am currently using the popen function in code that is compiled by two compilers: MS Visual Studio and gcc (on linux). I might want to add gcc (on MinGW) later.
The function is called popen for gcc, but _popen for MSVS, so i added the following to my source code:
#ifdef _MSC_VER
#define popen _popen
#define pclose _pclose
#endif
This works, but i would like to understand whether there exists a standard solution for such problems (i recall a similar case with stricmp/strcasecmp). Specifically, i would like to understand the following:
- Is
_MSC_VERthe right flag to depend on? I chose it because i have the impression that linux environment is “more standard”. - If i put these
#define‘s in some header file, is it important whether i#includeit before or afterstdio.h(for the case ofpopen)? - If
_popenis defined as a macro itself, is there a chance my#definewill fail? Should i use a “new” token likemy_popeninstead, for that reason or another? - Did someone already do this job for me and made a good “portability header” file that i can use?
- Anything else i should be aware of?
The way you are doing it is fine (with the
#ifdefetc) but the macro that you test isn’t.popenis something that depends on your operating system and not your compiler.I’d go for something like