What resources exist to aid in writing C/C++ code that works across multiple platforms and compilers? For example, I regularly find myself asking questions like:
有哪些资源可以帮助编写跨多个平台和编译器的C / C ++代码?例如,我经常发现自己提出的问题如下:
- Which preprocessor macros are automatically defined in various compilers and environments? (e.g.
__GCC__
,WIN32
,__WIN32__
,__CYGWIN__
) - Which versions of compilers and standard libraries support relatively new "standard" functions (e.g. the C99 long double trigonometric functions sinl(), cosl(), ...)
- What functions are available on different platforms for performing typical tasks, when no single portable function seems to exist? (e.g. getting the current time with sub-second precision)
在各种编译器和环境中自动定义哪些预处理器宏? (例如__GCC__,WIN32,__ WIN32__,__ CYGWIN__)
哪些版本的编译器和标准库支持相对较新的“标准”函数(例如C99长双三角函数sinl(),cosl(),...)
当没有单个便携式功能似乎存在时,在不同平台上可用于执行典型任务的功能是什么? (例如,以亚秒精度获得当前时间)
I often write code that should compile under Linux/gcc, cygwin, mingw32, and Visual Studio, and I frequently have to compare notes from multiple sources (Linux man pages, MSDN, compiler docs) to get the information I need. This must be a problem that developers run into all the time -- are there any resources that compile this information into an easily digestible reference?
我经常编写应该在Linux / gcc,cygwin,mingw32和Visual Studio下编译的代码,我经常需要比较来自多个源(Linux手册页,MSDN,编译器文档)的注释以获取我需要的信息。这一定是开发人员一直遇到的问题 - 是否有任何资源可以将这些信息编译成易于理解的引用?
(For this question I'm not particularly interested in cross-platform libraries like wxWidgets or boost. I'm more interested in resources or techniques that would help somebody write their own cross-platform library or program.)
(对于这个问题,我对wxWidgets或boost等跨平台库不是特别感兴趣。我对那些可以帮助某人编写自己的跨平台库或程序的资源或技术更感兴趣。)
EDIT: This is an example of the type of page I'm looking for: http://predef.sourceforge.net/precomp.html. A nice survey of various compilers/environments and the preprocessor macros that can be used to identify them. It would be great to find a similar resource that compared nearly-equivalent functions across platforms (like gmtime_r() or ftime() in Linux vs _gmtime_s() or _ftime() in Windows) when no common function exists.
编辑:这是我正在寻找的页面类型的示例:http://predef.sourceforge.net/precomp.html。对各种编译器/环境以及可用于识别它们的预处理器宏的一个很好的调查。如果不存在常用函数,那么找到一个类似的资源可以很好地比较跨平台的几乎等效的函数(例如Linux中的gmtime_r()或ftime()与Windows中的_gmtime_s()或_ftime()。
4 个解决方案
#1
Here is one resource that may be of use to you Pre-defined C/C++ Compiler Macros
这是一个可能对您使用的资源预定义的C / C ++编译器宏
#2
The basic way you go about writing cross-platform code is to write code that doesn't depend on specific platforms. For example, almost all the original UNIX utilities can be written without reference to a specific platform. Writing code that depends on specific macros by using conditional compilation is not best practice.
编写跨平台代码的基本方法是编写不依赖于特定平台的代码。例如,几乎所有原始UNIX实用程序都可以在不参考特定平台的情况下编写。使用条件编译编写依赖于特定宏的代码不是最佳实践。
#3
It really depends on your concrete project, but I would try to
这真的取决于你的具体项目,但我会尝试
- stick to ISO compliant code and POSIX if possible;
- use -Wall and eliminate all warnings;
- check out autoconf and the autotoolset whether they can help.
如果可能,坚持使用ISO兼容代码和POSIX;
使用-Wall并消除所有警告;
检查autoconf和autotoolset是否可以提供帮助。
#1
Here is one resource that may be of use to you Pre-defined C/C++ Compiler Macros
这是一个可能对您使用的资源预定义的C / C ++编译器宏
#2
The basic way you go about writing cross-platform code is to write code that doesn't depend on specific platforms. For example, almost all the original UNIX utilities can be written without reference to a specific platform. Writing code that depends on specific macros by using conditional compilation is not best practice.
编写跨平台代码的基本方法是编写不依赖于特定平台的代码。例如,几乎所有原始UNIX实用程序都可以在不参考特定平台的情况下编写。使用条件编译编写依赖于特定宏的代码不是最佳实践。
#3
It really depends on your concrete project, but I would try to
这真的取决于你的具体项目,但我会尝试
- stick to ISO compliant code and POSIX if possible;
- use -Wall and eliminate all warnings;
- check out autoconf and the autotoolset whether they can help.
如果可能,坚持使用ISO兼容代码和POSIX;
使用-Wall并消除所有警告;
检查autoconf和autotoolset是否可以提供帮助。