使用 Code::Blocks 编译时针对不同的 CPU - 理解错误

发布于 2024-12-08 15:35:52 字数 353 浏览 6 评论 0原文

我正在使用 Code::Blocks 开发一个 C 应用程序,并且需要面向多个平台(32 位、64 位)。我的开发盒是64位的,所以我尝试在项目设置中检查编译器的i386(-march=i386)选项。现在,当我编译时,我得到:

mainc:1: error: CPU you selected does not support x86-64 instructions set

  1. 我不明白这条消息!通过指定 i386 不应该只是生成 x86 指令(而不是 -64)吗?另外,我的CPU是X86-64,为什么说我的CPU不支持这个?

  2. 我这样做的方式错了吗?是否有针对不同架构的正确方法?

I am developing a C app using Code::Blocks, and need to target multiple platforms (32 bit, 64 bit). My development box is 64 bit, so I tried to check the i386 (-march=i386) option for the compiler in the project settings. Now when I compile I get:

mainc:1: error: CPU you selected does not support x86-64 instruction set

  1. I don't understand this message! By specifying i386 shouldn't it be just generating x86 instructions (not -64)? Beside, my CPU is X86-64 so why say my cpu doesn't support this?

  2. Am I doing this the wrong way? Is there a RIGHT way to target different architectures?

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

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

发布评论

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

评论(1

慵挽 2024-12-15 15:35:52

您必须在这里区分几件事:

  • 32 位和 64 位“平台”运行在相同处理器上。相同的 X86-64 处理器可以在 x86 或 AMD64 模式下运行。这决定了可用寄存器的数量、它们的大小、堆栈布局、不同的调用约定和一些不同的操作码(另请注意,在许多操作系统上,32 位代码将在 64 位操作系统中的某种兼容层中运行,而无需问题)
  • 有适用于其中一个平台的编译器,也有可以生成这两种平台中的任何一个的编译器(但不能同时生成这两种平台)。
  • 编译器可以是与程序构建的平台不同的平台(所谓的交叉编译器,这是非常常见的事情,例如在开发嵌入式系统时,但您也可以从 Linux-64 或 Windows-32 进行交叉编译)相反,或者在 Win32 计算机上为 Win64 进行编译)。
  • 除了平台之外,生成的代码中还存在特定于模型的差异(-march 开关)。这决定了假设的处理器世代,这决定了可以使用哪些指令来生成代码。并非每个平台和架构的组合都是有效的(这正是您的问题:假设使用了 25 年的 386 处理器意味着它不可能在 AMD64 模式下运行)。
  • 或者,您甚至可以安排与生成代码不同的内容。例如,您可以生成使用最高可达 Pentium III 指令的代码,但可为 Core i7 安排指令。这将在几乎每个处理器上运行(次优,但它将会运行),并且在最新的系列上以最佳方式运行。
  • 最后,还有许多开关,例如 -msse 系列,可以进一步启用/禁用指令。

您没有指定“Windows”或“Linux”或其他任何内容,因此很难对您的问题给出准确答案,但我们假设您可能使用例如 MingW-w64。该编译器允许您创建 32 位和 64 位代码,具体取决于您告诉它做什么。显然,如果你告诉它以 64 模式(默认!)生成代码,那么 i386 不是一个好的选择。如果你给它 -m32 作为命令行选项,它将生成 32 位代码,并且它会工作。

如果您准确地告诉您使用的编译器,可能会有助于找到更具体的答案。例如,Code::Blocks(可选)随 Windows 上的 TDM 4.5.1 版本一起提供,但使用其他操作系统下的任何内容。

You must distinguish several things here:

  • There are 32 and 64 bit "platforms" that run on the same processor. The same X86-64 processor can operate in either x86 or AMD64 mode. This determines among other things the number of available registers, their sizes, stack layout, different calling convention, and some different opcodes (also note that on many operating systems, 32 bit code will run in some kind of compatibility layer in a 64bit OS without problems)
  • There are compilers for either one of these platforms, and there are compilers which can produce either of both (but not both at the same time).
  • The compiler can be a different platform from what the program is built for (so-called cross-compiler, very common thing e.g. when developing for embedded systems, but you can also cross-compile for example for Windows-32 from Linux-64 or the other way around, or compile for Win64 on a Win32 computer).
  • In addition to the platform, there is a model-specific difference in generated code (-march switch). This decides what processor generation is assumed, which determines what instructions can be used in generating code. Not every combination of platform and architecture is valid (this is your very problem: assuming a 25 year old 386 processor means that it cannot possibly run in AMD64 mode).
  • Optionally, you can even schedule for something different than you generate code for. You can for example generate code that uses instructions for up to Pentium III, but schedule for Core i7. This will run (sub-optimally, but it will run) on pretty much every processor, and run optimally on the newest line.
  • Finally, there are many switches such as the -msse family which further enable/disable instructions.

You did not specify "Windows" or "Linux" or anything else, so it is hard to give an exact answer to what your problem is, but let's assume you might be using for example MingW-w64. This compiler lets you create both 32 and 64 bit code, depending on what you tell it to do. Obviously, if you tell it to generate code in 64 mode (the default!), then i386 is not a good choice. If you give it -m32 as a commandline option, it will generate 32bit code, and it will work.

It may help finding a more concrete answer if you tell exactly what compiler you use. Code::Blocks comes (optionally) with the TDM 4.5.1 build on Windows, for example, but uses whatever is there under other operating systems.

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