如何编写跨平台的c++我的 Mac 上有哪些程序?

发布于 2024-12-26 01:48:28 字数 502 浏览 3 评论 0原文

我的大学教授在他的c++课上给我们提出了一个要求:我们必须使用gnu C++编译器(GCC)来编写我们的程序。据我所知,安装 xcode 后,我得到了苹果版本的 gcc。但是,据我了解,这并不完全交叉兼容。所以我尝试使用mac ports安装gnu gcc编译器,但一直不成功。完成后:

port install gcc47

当我进入终端并运行时:

gcc47 -v

我得到“命令未找到” 当我运行:

gcc -v

我得到:

gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

显示我仍在使用苹果的版本。我不明白的部分是:

  1. 如何安装gcc(gnu版本,不是苹果版本)
  2. 如何与ide一起使用,例如eclipse?

My university professor has given a requirement for us in his c++ class: We must write our programs using the gnu C++ compiler (GCC). I understand that after installing xcode, I get an apple version of gcc. However, this is not fully cross compatible as I understand. So I have tried to install the gnu gcc compiler by using mac ports, but I have been unsuccessful. After doing:

port install gcc47

When I go to the terminal and run:

gcc47 -v

I get "command not found"
When I run:

gcc -v

I get:

gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

Showing that I am still using apple's version. The part I am not understanding is:

  1. How do I install gcc (the gnu version, not apple's version)
  2. How do I use it with an ide, such as eclipse?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

万劫不复 2025-01-02 01:48:28

您使用的 GCC 版本(Apple 或 gnu 本机)应该与您继续操作无关。

编写可移植的 C++ 与编译器无关——除了确保编译器最好地遵守 C++ 标准之外。 幸运的是,GCC 是最好的 C++ 编译器之一。

您本身并不打算进行跨平台 C++,因为考虑到各种不同的环境,真正的可移植 C++ 编写起来有些痛苦。各种系统的 C++ 编译器的状态(例如 VAX/VMS 上的 CC 不支持模板...)。我怀疑你想要做的是确保你在 Mac 上编写的代码可以由你的教授编译。只要 GCC 可以在你的 mac 上处理它,就应该没问题。

因此,对于您的具体问题:

您的第一个问题是不必要的,因为您可以使用苹果提供的 g++ 来编译您的代码。

对于你的第二个问题,我强烈建议不要在尝试学习时使用 IDE。 IDE 提供了一些非常节省时间的功能,但它们隐藏了一些重要的方面,我相信这些方面值得您在学习过程的早期努力解决,这将有助于增强您的 C++ 技能。虽然情况确实有所改善,但某些 IDE 因创建不可移植的 C++(即使用 void main())而臭名昭著。

如果您仍然决定使用 Eclipse 或 XCode,因为使用哪个 GCC 安装并不重要,默认设置应该可以正常工作。

The version of GCC you use (Apple or gnu native) should be irrelevant for you to proceed.

Writing portable C++ has nothing to do with the compiler--outside of ensuring your compiler does it's best to adhere to the C++ standard. Luckily enough, GCC is one of the best C++ compilers out there.

You're not looking to do cross-platform C++ per-se, because true portable C++ is somewhat of a pain to write given the various state of C++ compilers for various systems (for example the CC on VAX/VMS doesn't support templates...). I suspect what you want to do is ensure the code you write on your mac, can be compiled by your prof. As long as GCC can handle it on your mac, it should be fine.

So to your specific questions:

Your first question is unnecessary, as you can use apple's supplied g++ to compile your code.

For your second question, I would highly recommend against using an IDE while trying to learn. IDE's offer some really great time saving features, but they hide some important aspects, that I believe are worth struggling with early in your learning process, and which will help strengthen your C++ skills. And while things have certainly have gotten better, some IDE's were notorious for creating non-portable C++ (ie using void main()).

If you are still set on using Eclipse, or XCode, since it doesn't matter which GCC installation you use, the default setup should work just fine.

旧人哭 2025-01-02 01:48:28

我过去在通过端口安装 gcc47 时遇到了麻烦,但 gcc46 按预期进行。您可能还想安装gcc_select

从那里,您可以使用 gcc_select,或在 Eclipse 的工具链编辑器中显式指定路径。

其他说明:

  • 如果您不需要 C++11 功能,那么 Apple 的 GCC 4.2.1 应该可以正常工作。也许您可以详细说明为什么不能使用它。基本上,Apple 在某些情况下向工具链添加了一些扩展(标记为 APPLE_ONLY),并且您可以很好地控制启用/禁用哪些选项。

  • 随 Xcode 一起提供的最新版本的 Clang 可以很好地处理 C++(包括对 C++11 的一些支持)。使用 Clang 时,我会怀念 GCC 中的一些高级功能,但 Clang 目前的 C++ 支持确实非常好。

I've run into troubles installing gcc47 via ports in the past, but gcc46 went as expected. You may also want to install gcc_select.

From there, you can use gcc_select, or specify the path explicitly in Eclipse's toolchain editor.

Other notes:

  • If you don't need C++11 features, then Apple's GCC 4.2.1 should work fine. Perhaps you can detail why you can't use it. Basically, Apple's added some extensions to the toolchain in some cases (marked APPLE_ONLY), and you have good control of what options are enabled/disabled.

  • Recent versions of Clang which ship with Xcode handle C++ well (including some support for C++11). There are some advanced things in GCC that I miss when using Clang, but Clang's current C++ support is really quite good.

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