如何制作 C++跨编译器的程序工作
我想知道如何让我的 C++ 程序跨编译器工作。我想要制作这个程序,如果它是用 borland 编译的,它将使用 clrscr() 函数,否则它将使用 system("CLS") 。我见过做过类似事情的代码,但我找不到它的作用或工作原理的解释。任何帮助将不胜感激。
I wanted to know how I would make my C++ program work across compilers. I wanted to make the program so if it's being compiled with borland it will use the clrscr()
function otherwise it'd use system("CLS")
. I've seen code that has done something similar but I couldn't find an explanation of what it does or how it works. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一般来说,要使 C 或 C++ 程序跨多个编译器工作,您需要尽可能地将自己限制在标准 C 或 C++ 上。不过,有时您必须使用编译器/平台特定的功能,处理该问题的一种方法是通过预处理器。
SourceForge 上的 predef 项目列出了一堆由各种编译器针对各种平台自动定义的预处理器符号,等等等等。您可以使用该信息来实现您所需要的,例如:
In general, to make a C or C++ program work across multiple compilers you want to confine yourself to standard C or C++ as much as possible. Sometimes you have to use compiler/platform specific functionality, though, and one way to handle that is via the preprocessor.
The predef project on SourceForge lists a bunch a preprocessor symbols that are defined automatically by various compilers, for various platforms, et cetera. You can use that information to implement what you need, for example:
从头到尾的一个简单答案是定义您自己的函数调用,然后根据编译参数将其转换为实际调用(使用 #ifdef 预处理定义 - 查看哪些值对应于哪个编译器)。
例如:
当然,您必须为此创建一个专用头文件并将其包含在各处。
(当然,您必须用相关定义替换 COMPILER_ONE 和 COMPILER_TWO :))
One easy answer from the top of the head is define your own function calls and then translate it into real calls depending on the compiling parameters (with #ifdef preprocessing definitions - look which values are corresponding to which compiler).
example:
You would have to create a dedicated header file for this and to include it everywhere, of course.
(Of course you have to substitute COMPILER_ONE and COMPILER_TWO with relevant definitions :) )
如何让某些东西在不同的编译器上工作是一个简单的问题,但回答起来非常复杂!您关于清除屏幕的具体询问;
我会这样尝试,首先你有自己的函数说
并像这样定义它:
请注意我刚刚猜到了 #define 是什么。这被称为条件并发症,通常被认为是邪恶的,但将其包含在函数中会稍微减少危害。
How to make something work across different compilers is simple question which is very complex to answer! Your specific query about clearing the screen;
I would attempt it like this, first you have your own function say
And define it like this:
Please note I have just guessed what the #define 's are. This is know as conditional complication, generally regarded as evil, but containing it in a function reduces the harm a little.
它通常完成的方式是通过预处理器或 makefile 的魔力。无论哪种方式,您都可以将实现细节隐藏在头文件中的公共接口后面,例如
voidclearscreen()
。然后,在单个源文件中,您可以将 Borland 实现隐藏在 #ifdef BORLAND 后面,对于其他实现也类似。或者,您可以将每个实现放在单独的源文件中,并且仅根据 makefile 中的变量编译正确的实现。The way it's typically done is through the magic of the preprocessor or makefiles. Either way, you hide the implementation details behind a common interface in a header file, such as
void clearscreen()
. Then in a single source file you can hide the Borland implementation behind #ifdef BORLAND, and similarly for other implementations. Alternatively, you can put each implementation in a separate source file, and only compile the proper one based on a variable in a makefile.您可以通过使用
#ifdef
编译器宏检查编译器宏来完成此操作:You can do this by checking compiler macros with the
#ifdef
compiler macro: