如何制作 C++跨编译器的程序工作

发布于 2024-10-15 09:57:23 字数 138 浏览 2 评论 0原文

我想知道如何让我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三人与歌 2024-10-22 09:57:23

一般来说,要使 C 或 C++ 程序跨多个编译器工作,您需要尽可能地将自己限制在标准 C 或 C++ 上。不过,有时您必须使用编译器/平台特定的功能,处理该问题的一种方法是通过预处理器。

SourceForge 上的 predef 项目列出了一堆由各种编译器针对各种平台自动定义的预处理器符号,等等等等。您可以使用该信息来实现您所需要的,例如:

void clearScreen() {
  // __BORLANDC__ is defined by the Borland C++ compiler.
  #ifdef __BORLANDC__
    clrscr();
  #else
    system("cls");
  #endif
}

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:

void clearScreen() {
  // __BORLANDC__ is defined by the Borland C++ compiler.
  #ifdef __BORLANDC__
    clrscr();
  #else
    system("cls");
  #endif
}
夜夜流光相皎洁 2024-10-22 09:57:23

从头到尾的一个简单答案是定义您自己的函数调用,然后根据编译参数将其转换为实际调用(使用 #ifdef 预处理定义 - 查看哪些值对应于哪个编译器)。
例如:

#if defined(__COMPILER_ONE__)
#define ClearScreen() clrscr()
#elif defined(__COMPILER_TWO__)
#define ClearScreen() system("CLS")
#else 
#error "I do not know what to do!"
#endif

当然,您必须为此创建一个专用头文件并将其包含在各处。

(当然,您必须用相关定义替换 COMPILER_ONECOMPILER_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:

#if defined(__COMPILER_ONE__)
#define ClearScreen() clrscr()
#elif defined(__COMPILER_TWO__)
#define ClearScreen() system("CLS")
#else 
#error "I do not know what to do!"
#endif

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 :) )

韶华倾负 2024-10-22 09:57:23

如何让某些东西在不同的编译器上工作是一个简单的问题,但回答起来非常复杂!您关于清除屏幕的具体询问;

我会这样尝试,首先你有自己的函数说

      void clear_screen();

并像这样定义它:

      void clear_screen()
      {
        #ifdef LINUX
           ...

        #eleif MS_WIN

          ...
        #endif
      }

请注意我刚刚猜到了 #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

      void clear_screen();

And define it like this:

      void clear_screen()
      {
        #ifdef LINUX
           ...

        #eleif MS_WIN

          ...
        #endif
      }

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.

薆情海 2024-10-22 09:57:23

它通常完成的方式是通过预处理器或 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.

故事灯 2024-10-22 09:57:23

您可以通过使用 #ifdef 编译器宏检查编译器宏来完成此操作:

#ifdef BORLAND
    borland();
#else
    otherCompiler();
#endif

You can do this by checking compiler macros with the #ifdef compiler macro:

#ifdef BORLAND
    borland();
#else
    otherCompiler();
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文