Question:
when you write programs in a linux environment how does the gcc compiler know what assembly code to produce?
SailorTwift
2015-01-08 15:59:51 UTC
Since hardware architectures are different in most computers how does the gcc compiler 'know' what assembly language to produce, since the assembly code needs to be compatible with that specific architecture.

for example if my computer has a IA32 architecture and I write a c program, and then gcc it, how does the gcc compiler know to produce IA32 instructions and not instructions in a different instruction set, like MIPS, or Y86.
Six answers:
2015-01-08 16:31:10 UTC
to answer your question directly...

the gcc program (gnu c compiler) is the name of an executable that was compiled for your architecture.

Thus, at it's own compilation time (the time of it's manufacture), was specificied to work on a x86.



If you have the gcc source, you may make a compiler yourself (good luck), which 'runs' on your x86 and compiles 'mips'.

to quote (http://unix.stackexchange.com/questions/163241/generate-mips-architecture-assembly-code-on-a-x86-machine )

"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. Or in layman's terms, this cross compilation toolchain is similar to tweaking the petrol engine to run the diesel car."



=========

it is hard to even get a operation-override function to work... so allow me to delete this once you google "preparing a cross-compilation toolchain"
Neerp
2015-01-09 07:48:41 UTC
First of all, hardware architectures are not different in most computers as far as the compiler is concerned. While gcc supports 50 or 60 architectures or so, most PCs are i386 or ia64 IIRC. Gcc knows what your machine is, and compiles for your machine by default. Look here for a list of supported architectures:



http://gcc.gnu.org/backends.html
Tom
2015-01-10 22:10:50 UTC
You specify your architecture and ABI in your makefile.

GCC supports cross compiling to many architectures.
efflandt
2015-01-08 16:12:13 UTC
Open a text terminal (in X or console or ssh to that system or whatever) type "man gcc" (without quotes), hit Enter key, and read that using cursor keys for enlightenment.



If you are not compiling it for the system operating you are compiling it on, you have to tell it what options you want in the command line.
adaviel
2015-01-08 17:15:51 UTC
If you want to cross-compile, use the "-b" option. As detailed in the manpage. There are other options for each hardware family, e.g compiling for older Intel CPUs without newer instructions.

See "gcc --target-options"
Chris
2015-01-08 16:02:13 UTC
I strongly suspect that gcc supports tons of command line arguments. You can simply tell it which code to produce.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...