C语言 有没有办法使用 gcc 将 C 转换为 MIPS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4175450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:03:02  来源:igfitidea点击:

Is there a way to use gcc to convert C to MIPS?

cgcccompiler-constructionmips

提问by Mike

I completed a C to MIPS conversion for a class, and I want to check it against the assembly. I have heard that there is a way of configuring gcc so that it can convert C code to the MIPS architecture rather than the x86 architecture (my computer users an Intel i5 processor) and prints the output.

我完成了一个类的 C 到 MIPS 的转换,我想根据程序集检查它。我听说有一种配置 gcc 的方法,以便它可以将 C 代码转换为 MIPS 架构而不是 x86 架构(我的计算机使用 Intel i5 处理器)并打印输出。

Running the terminal in Ubuntu (which comes with gcc), what command do I use to configure gcc to convert to MIPS? Is there anything I need to install as well?

在Ubuntu(gcc自带)中运行终端,我用什么命令来配置gcc转换为MIPS?还有什么我需要安装的吗?

EDIT: Let me clarify. Please read this. I'm not looking for which compiler to use, or people saying "well you could cross-compile, but instead you should use this other thing that has no instructions on how to set up."

编辑:让我澄清一下。请阅读这个。我不是在寻找要使用的编译器,也不是人们在说“好吧,您可以交叉编译,但是您应该使用其他没有关于如何设置的说明的东西。”

If you're going to post that, at least refer me to instructions. GCC came with Ubuntu. I don't have experience on how to install compilers and it's not easy finding online tutorials for anything other than GCC. Then there's the case of cross-compiling I need to know about as well. Thank you.

如果您要发布该内容,请至少让我参考说明。GCC 随 Ubuntu 一起提供。我没有关于如何安装编译器的经验,而且要找到 GCC 以外的任何其他内容的在线教程并不容易。然后还有我需要了解的交叉编译的情况。谢谢你。

回答by Thomas Pornin

GCC canproduce assembly code for a large number of architectures, include MIPS. But what architecture a given GCC instance targets is decided when GCC itself is compiled. The precompiled binary you will find in an Ubuntu system knows about x86 (possibly both 32-bit and 64-bit modes) but not MIPS.

GCC可以为大量架构生成汇编代码,包括 MIPS。但是当 GCC 本身被编译时,一个给定的 GCC 实例所针对的架构是决定的。您将在 Ubuntu 系统中找到的预编译二进制文件了解 x86(可能包括 32 位和 64 位模式)但不了解 MIPS。

Compiling GCC with a target architecture distinct from the architecture on which GCC itself will be running is known as preparing a cross-compilation toolchain. This is doable but requires quite a bit of documentation-reading and patience; you usually need to first build a cross-assembler and cross-linker (GNU binutils), then build the cross-GCC itself.

使用不同于 GCC 本身将运行的架构的目标架构编译 GCC 被称为准备交叉编译工具链。这是可行的,但需要相当多的文档阅读和耐心;您通常需要首先构建交叉汇编器和交叉链接器(GNU binutils),然后构建跨 GCC 本身。

I recommend using buildroot. This is a set of scripts and makefiles designed to help with the production of a complete cross-compilation toolchain and utilities. At the end of the day, you will get a complete OS and development tools for a target system. This includes the cross-compiler you are after.

我建议使用buildroot。这是一组脚本和生成文件,旨在帮助生成完整的交叉编译工具链和实用程序。最终,您将获得用于目标系统的完整操作系统和开发工具。这包括您所追求的交叉编译器。

Another quite different solution is to use QEMU. This is an emulator for various processors and systems, including MIPS systems. You can use it to run a virtual machine with a MIPS processor, and, within that machine, install an operating system for MIPS, e.g. Debian, a Linux distribution. This way, you get a nativeGCC (a GCC running on a MIPS system and producing code for MIPS).

另一个完全不同的解决方案是使用QEMU。这是一个适用于各种处理器和系统的模拟器,包括 MIPS 系统。您可以使用它来运行带有 MIPS 处理器的虚拟机,并在该机器内为 MIPS 安装操作系统,例如Debian,Linux 发行版。通过这种方式,您可以获得本机GCC(在 MIPS 系统上运行并为 MIPS 生成代码的 GCC)。

The QEMU way might be a tad simpler; using cross-compilation requires some understanding of some hairy details. Either way, you will need about 1 GB of free disk space.

QEMU 方式可能更简单一些;使用交叉编译需要对一些毛茸茸的细节有所了解。无论哪种方式,您都需要大约 1 GB 的可用磁盘空间。

回答by Fabian Giesen

It's not a configuration thing, you need a version of GCC that cross-compiles to MIPS. This requires a special GCC build and is quitehairy to set up (building GCC is not for the faint of heart).

这不是配置问题,您需要一个交叉编译为 MIPS 的 GCC 版本。这需要一个特殊的 GCC 构建并且设置起来非常麻烦(构建 GCC 不适合胆小的人)。

I'd recommend using LCCfor this. It's way easier to do cross-compilation with LCC than it is with GCC, and building LCC is a matter of seconds on current machines.

我建议为此使用LCC。与 GCC 相比,使用 LCC 进行交叉编译要容易得多,并且在当前机器上构建 LCC 只需几秒钟。

回答by Ben Hymanson

You would need to download the source to binutils and gcc-core and compile with something like ../configure --target=mips .... You may need to choose a specific MIPS target. Then you could use mips-gcc -S.

您需要将源代码下载到 binutils 和 gcc-core 并使用类似../configure --target=mips .... 您可能需要选择特定的 MIPS 目标。然后你可以使用mips-gcc -S.

回答by Nils Pipenbrinck

You can cross-compile the GCC so that it generates MIPS code instead of x86. That's a nice learning experience.

您可以交叉编译 GCC,使其生成 MIPS 代码而不是 x86。这是一次很好的学习经历。

If you want quick results you can also get a prebuilt GCC with MIPS support. One is the CodeSourcery Lite Toolchain. It is free, comes for a lot of architectures (including MIPS) and they have ready to use binaries for Linux and Windows.

如果您想快速获得结果,您还可以获得支持 MIPS 的预构建 GCC。一种是 CodeSourcery Lite 工具链。它是免费的,适用于许多架构(包括 MIPS),并且他们已经准备好使用 Linux 和 Windows 的二进制文件。

http://www.codesourcery.com/sgpp/lite/mips/portal/subscription?@template=lite

http://www.codesourcery.com/sgpp/lite/mips/portal/subscription?@template=lite

回答by Flux

You should install a cross-compiler from the Ubuntu repositories. There are four GCC MIPS C cross-compilers provided in the repositories. Pick according to your needs:

您应该从 Ubuntu 存储库安装交叉编译器。存储库中提供了四个 GCC MIPS C 交叉编译器。根据您的需求选择:

  • gcc-mips-linux-gnu- 32-bit big-endian.
  • gcc-mipsel-linux-gnu- 32-bit little-endian.
  • gcc-mips64-linux-gnuabi64- 64-bit big-endian.
  • gcc-mips64el-linux-gnuabi64- 64-bit little-endian.
  • gcc-mips-linux-gnu- 32 位大端。
  • gcc-mipsel-linux-gnu- 32 位小端。
  • gcc-mips64-linux-gnuabi64- 64 位大端。
  • gcc-mips64el-linux-gnuabi64- 64 位小端。

(Note for Debian users: if you usually like to install your regular compilers using the build-essentialpackage, you would be interested to know of the existence of crossbuild-essential-mips, crossbuild-essential-mipseland crossbuild-essential-mips64el)

(Debian 用户注意事项:如果您通常喜欢使用该build-essential软件包安装常规编译器,您会很想知道crossbuild-essential-mips,crossbuild-essential-mipsel和的存在crossbuild-essential-mips64el

In the following examples, I will assume that you chose the 32-bit little-endian version (sudo apt-get install gcc-mipsel-linux-gnu). The commands for other MIPS versions are similar.

在以下示例中,我将假设您选择了 32 位小端版本 ( sudo apt-get install gcc-mipsel-linux-gnu)。其他 MIPS 版本的命令类似。

To deal with MIPS instead of the native architecture of your system, use the mipsel-linux-gnu-gcccommand instead of gcc. For example, mipsel-linux-gnu-gcc -fverbose-asm -S myprog.cproduces a file myprog.scontaining MIPS assembly.

要处理 MIPS 而不是系统的本机体系结构,请使用mipsel-linux-gnu-gcc命令而不是gcc. 例如,mipsel-linux-gnu-gcc -fverbose-asm -S myprog.c生成一个myprog.s包含 MIPS 程序集的文件。

Another way to see the MIPS assembly: run mipsel-linux-gnu-gcc -g -c myprog.cto produce an object file myprog.othat contains debugging information. Then view the disassembly of the object file using mipsel-linux-gnu-objdump -d -S myprog.o. For example, if myprog.cis this:

查看 MIPS 程序集的另一种方法:运行mipsel-linux-gnu-gcc -g -c myprog.c以生成myprog.o包含调试信息的目标文件。然后使用mipsel-linux-gnu-objdump -d -S myprog.o. 例如,如果myprog.c是这样:

#include <stdio.h>

int main()
{
    int a = 1;
    int b = 2;
    printf("The answer is: %d\n", a + b);
    return 0;
}

And if it is compiled using mipsel-linux-gnu-gcc -g -c myprog.c, then mipsel-linux-gnu-objdump -d -S myprog.owill show something like this:

如果它是使用 编译的mipsel-linux-gnu-gcc -g -c myprog.cmipsel-linux-gnu-objdump -d -S myprog.o则将显示如下内容:

myprog.o:     file format elf32-tradlittlemips


Disassembly of section .text:

00000000 <main>:
#include <stdio.h>

int main() {
   0:   27bdffd8    addiu   sp,sp,-40
   4:   afbf0024    sw  ra,36(sp)
   8:   afbe0020    sw  s8,32(sp)
   c:   03a0f025    move    s8,sp
  10:   3c1c0000    lui gp,0x0
  14:   279c0000    addiu   gp,gp,0
  18:   afbc0010    sw  gp,16(sp)
    int a = 1;
  1c:   24020001    li  v0,1
  20:   afc20018    sw  v0,24(s8)
    int b = 2;
  24:   24020002    li  v0,2
  28:   afc2001c    sw  v0,28(s8)
    printf("The answer is: %d\n", a + b);
  2c:   8fc30018    lw  v1,24(s8)
  30:   8fc2001c    lw  v0,28(s8)
  34:   00621021    addu    v0,v1,v0
  38:   00402825    move    a1,v0
  3c:   3c020000    lui v0,0x0
  40:   24440000    addiu   a0,v0,0
  44:   8f820000    lw  v0,0(gp)
  48:   0040c825    move    t9,v0
  4c:   0320f809    jalr    t9
  50:   00000000    nop
  54:   8fdc0010    lw  gp,16(s8)
    return 0;
  58:   00001025    move    v0,zero
}
  5c:   03c0e825    move    sp,s8
  60:   8fbf0024    lw  ra,36(sp)
  64:   8fbe0020    lw  s8,32(sp)
  68:   27bd0028    addiu   sp,sp,40
  6c:   03e00008    jr  ra
  70:   00000000    nop
    ...

回答by Hyman

You should compile your own version of gcc which is able to cross-compile. Of course this ain't easy, so you could look for a different approach.. for example thisSDK.

您应该编译自己的能够交叉编译的 gcc 版本。当然这并不容易,因此您可以寻找不同的方法.. 例如这个SDK。