The most standard way to choose a function name depending on the platform?

I am currently using a function popenin 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 popenfor gcc, but _popenfor 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 if there is a standard solution for such problems (I recall a similar case with stricmp/ strcasecmp). In particular, I would like to understand the following:

  • Is the _MSC_VERright flag? I chose it because I have the impression that the Linux environment is more "standard".
  • If I put these #definein some header file, is it important that I am #includebefore or after stdio.h(for the case popen)?
  • If _popendefined as a macro itself, is there a chance that mine #definewill fail? Should I use a “new” token, for example my_popen, for one reason or another?
  • Has someone already done this job for me and created a nice "portability header" file that I can use?
  • Anything else I should know about?
+5
source share
4 answers

, , ( #ifdef ..), , , . popen - , , .

-

#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 2)
/* system has popen as expected */
#elif defined(YOUR_MACRO_TO DETECT_YOUR_OS)
# define popen _popen
# define pclose _pclose
#elif defined(YOUR_MACRO_TO DETECT_ANOTHER_ONE)
# define popen _pOpenOrSo
# define pclose _pclos
#else
# error "no popen, we don't know what to do"
#endif
+1
+3
  • _MSC_VER - MSVC. __GNUC__ GCC.

  • popen , #include , - 3.

  • #include stdio.h, AFAIK, , , ? portable_popen - .

  • ( ) , . - , . , ( , ), , .

  • , . , .

+1

, , #ifdef.. #else.. #endif, , :

  • #define my_popen
  • #include
  • , #define (.. my_popen)
  • , (, config/windows/mydefines.h config/linux/mydefines.h linux, #include "mydefines.h")

, .

, , - linux, , , (.. , linux), - . mydefines.h, myfunctions.c, config/OSTYPE.

, , linux Windows: , Linux Windows .

0

All Articles