English 中文(简体)
根据平台选择职能名称的多数标准方式?
原标题:Most standard way to select a function name depending on platform?

目前,我正在使用由两家汇编者编成的代码<代码>popen功能:MS视觉演播室和gcc(按纬度)。 稍后,我不妨加上“cc”。

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

这样做,但我想理解是否存在这些问题的标准解决办法(即回顾与<条码>条码/代码>/<条码>的类似情况。 具体来说,我想理解以下几点:

  1. Is _MSC_VER the right flag to depend on? I chose it because i have the impression that linux environment is "more standard".
  2. If i put these #define s in some header file, is it important whether i #include it before or after stdio.h (for the case of popen)?
  3. If _popen is defined as a macro itself, is there a chance my #define will fail? Should i use a "new" token like my_popen instead, for that reason or another?
  4. Did someone already do this job for me and made a good "portability header" file that i can use?
  5. Anything else i should be aware of?
最佳回答

您的工作方式是罚款(#ifdef等),但你测试的宏观标准是t.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. Better to check for a windows-specific define (_WIN32 perhaps) because mingw won t have it either. popen() is standardised (it s a part of the Single UNIX® Specification v2)
  2. No; so long as the macro is defined before its first use it does not matter if _popen() is not defined until later.
  3. No; what you have is fine even if _popen is a macro.
  4. It s been done many times but I don t know of a freely-licensed version you can use.
  1. _MSC_VER 是检测MSVC汇编器的正确宏观。 您可使用海合会的_GNUC_

  2. If you are going to use popen as your macro ID, I suggest you #include it after, because of 3.

  3. 如果在<代码>stdio.h之后插入“编号>#include,则该编码应当运行AFAIK,但应当比音响安全,而不是安全吗? 电话:portable_popen<>/code>或东西。

  4. 许多项目(包括一些地雷)都有可携带的头盔,但通常更能自行推出。 如果你有时间,我会 yourself一刀。 因此,你知道你的法典的细节(如果情况有错的话,可以加以 de弄),并且你制定了符合你们需要的法典。

  5. 我不知道这一点。 我完全像现在这样,没有问题。

而不是用包含<代码>#ifdef的封面文档结束。 简编

  • put the OS dependent definitions in one file per platform and #define a macro my_popen
  • #include this file in your platform-agnostic code
  • never call the OS functions directly, but the #define that you created (i.e. my_popen)
  • depending on your OS, use different headers for compilation (e.g. config/windows/mydefines.h on windows and config/linux/mydefines.h on linux, so set the include path appropriate and always #include "mydefines.h")

That s a much cleaner approach than having the OS decision in the source itself.

如果你要求采用的方法在窗户和中子之间有所不同,则决定什么是你重新使用的行为(即:always 窗户行为或always linux conduct),然后为达到这一目的创造适当的方法。 为此,您不仅需要两份<密码>-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

这样,在传播中子和窗户版本时,你也获得了好处:你可以简单地复制两个档案,同时在same文档的中子和窗户上填写小块。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签