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

时间:2022-05-15 03:13:53

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之外,找不到其他任何内容的在线教程并不容易。然后是我需要知道的交叉编译的情况。谢谢。

5 个解决方案

#1


26  

GCC can produce 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。这是一组脚本和makefile,旨在帮助生成完整的交叉编译工具链和实用程序。在一天结束时,您将获得目标系统的完整操作系统和开发工具。这包括您追求的交叉编译器。

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 native GCC (a GCC running on a MIPS system and producing code for MIPS).

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

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的可用磁盘空间。

#2


5  

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 quite hairy to set up (building GCC is not for the faint of heart).

这不是配置,你需要一个可以交叉编译为MIPS的GCC版本。这需要一个特殊的GCC构建,并且设置非常多毛(构建GCC不适合胆小的人)。

I'd recommend using LCC for 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。使用LCC进行交叉编译比使用GCC更容易,并且在当前机器上构建LCC只需几秒钟。

#3


2  

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。

#4


1  

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

#5


0  

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 this SDK.

你应该编译自己的gcc版本,它能够交叉编译。当然这并不容易,所以你可以寻找一种不同的方法......比如这个SDK。

#1


26  

GCC can produce 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。这是一组脚本和makefile,旨在帮助生成完整的交叉编译工具链和实用程序。在一天结束时,您将获得目标系统的完整操作系统和开发工具。这包括您追求的交叉编译器。

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 native GCC (a GCC running on a MIPS system and producing code for MIPS).

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

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的可用磁盘空间。

#2


5  

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 quite hairy to set up (building GCC is not for the faint of heart).

这不是配置,你需要一个可以交叉编译为MIPS的GCC版本。这需要一个特殊的GCC构建,并且设置非常多毛(构建GCC不适合胆小的人)。

I'd recommend using LCC for 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。使用LCC进行交叉编译比使用GCC更容易,并且在当前机器上构建LCC只需几秒钟。

#3


2  

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。

#4


1  

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

#5


0  

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 this SDK.

你应该编译自己的gcc版本,它能够交叉编译。当然这并不容易,所以你可以寻找一种不同的方法......比如这个SDK。