Compilers

Would articles for specific compiler tutorials be helpful on this website?

  • Yes

    Votes: 0 0.0%
  • No

    Votes: 0 0.0%

  • Total voters
    0
D

DevynCJohnson

Guest
Many developers use Linux. Considering that Linux itself and nearly all of the software is open source (unless the user installs proprietary software), software development is important to the Linux community. One of the most important parts of programming is the compiler. A compiler gets the code and creates the final product. This final object may be an executable file, a library, driver, etc. Compilers use linkers to connect generated object files, parsers check the code for errors, etc. There are may steps involved in compiling code. However, the purpose of this article is to understand how a programmer would use a compiler, the types of compilers, and what compilers are available for Linux.

Types
In simple terms, a compiler typically converts higher-level programming languages to a lower-level languages. For example, many C/C++ compilers get C/C++ code and create an executable that contains machine-code (which is more low-level than assembly).

A decompiler converts a low-level language (such as machine-code) to a higher-level languages (like assembly or C/C++).

A source-to-source compiler (commonly called a "transpiler") converts between languages. For example, the "coffee" transpiler converts Coffeescript to JavaScript.

A cross-compiler creates machine code for a different machine.

A rewriter changes some expressions, commands, symbols, etc. in the source code, but the source code is still the same language.

Compilers
The GNU Toolchain is a popular development suite containing many tools. A subset of this toolchain is called the GNU Compiler Collection and it contains some compilers. "gcc" (lowercase letters) is the compiler for C code and g++ compiles C++ code. The compilers in the GNU Compiler Collection (like most compilers) are not single programs. Rather, the g++ compiler is a frontend that informs the GNU Toolchain that C++ code is intended to be compiled. The system has a default linker, parser, assembler, lexer, etc. that it will use. However, the user may install other tools and use parameters in the command-line to specify which tools should be used.

The basic usage of the GNU compilers looks like this - "g++ -c myprog.cpp". That command compiles the specified C++ source code file to an object file. Next, type "g++ -o ./Program ./myprog.o" which will perform some more actions to create the final program, designated as "-o ./Program". Many other parameters exist which can specify many different behaviors and outcomes. For instance, to include certain libraries or indicate the location of certain libraries, use the “-I” parameter for each specified path. For instance, "g++ -I/usr/lib/include/my-libs -c myprog.cpp" is a suitable example. Notice that there is no space between the “-I” parameter and the path. Many (or most) compilers use such a format.

Some other GNU compiler wrappers are available. The "Go" programming language is compiled with gogcc, and Java is compiled to class files with gcj. Other supported wrappers include gdc (D), ghc (Haskell), ghdl (vhdl), gpc (Pascal), gnat (Ada), gfortran (Fortran), g77 (deprecated Fortran compiler), gobjc (Objective-C), and JGNAT (Ada to Java bytecode).

NOTE: ghc = Glasgow Haskell Compiler

The GNU Compiler Collection supports more languages than mentioned. To compile such languages, test if "gcc" supports the language already without needing a wrapper or special parameter. If not, than install any needed libraries, extensions, compilers, wrappers, etc. Also, be aware if the language must be specified via a parameter. For example, to compile Objective-C with gcc, type a command similar to any of the commands below.

Code:
gcc -c prog.m
gcc -lobjc -o Program prog.m

JGNAT is a form of GNAT that compiles Ada to Java Bytecode. Another form of GNAT includes "GNAT for dotNET" which compiles Ada for the .NET Framework.

NOTE: The GNU Interpreter for Java (GIJ) is an interpreter for Java bytecode. This program is not a compiler.

Gambas is a form of BASIC. Gambas and its IDE (sometimes under the same name or named "Gambas3") resemble Visual Basic and Microsoft's standard Visual Basic IDE. The Gambas compiler for the command-line is "gbc3".

Clang is a C/C++ and Objective C/C++ compiler that uses LLVM as its backend. Clang is a comparable alternative to GCC. LLVM (Low Level Virtual Machine) manages the optimizations and various other tasks.

Vala developers using GTK as the GUI may find "gtkamlc" helpful. "gtkamlc" is a compiler for both the GtkON language and the Gtkaml language. With these languages and the compiler, the GTK code will be compact in the Vala program.

Numerous other compilers are available for Linux for a variety of programming languages. Some are seen below.

  • valac - Vala
  • booc - Boo
  • llc - LLVM Compiler for LLVM bitcode/bytecode (*.bc) or LLVM assembly (*.ll)
  • mcs - Mono C#
  • luac - Compiler Lua to bytecode
  • javac - Compile Java files (*.java) to Java class files (*.class)
  • emcc - Emscripten frontend (LLVM bytecode to JavaScript); this is a transpiler
  • gcl - Common Lisp
  • wxrc - WxWidget XML compiler
  • ocamlc - Ocaml
  • groovyc - Groovy
  • cxfreeze - Python; create stand-alone binary executables from scripts written in Python.
  • Nuitka - Python compiler
  • scalac - compile Scala to Java bytecode (class files)

License Compiler (Mono)
"lc" is a license compiler for the Mono framework. Mono is a framework that provides the .NET Framework, the Common Language Runtime virtual/runtime machine, and C# tools to Linux, BSD, Solaris, and other systems. The license compiler allows a software license (as "licenses.licx") for proprietary code to be embedded into the program that will be compiled. For illustration, a software developer may write a program in .NET using Mono on a Linux system. The developer intends to sell the program. The license agreement (like the EULA licenses common to Windows) is then created as a "licenses.licx", and "lc" compiles the file. Then, when the whole program is to be compiled, the compiler will get the file create by "lc".

Compiler Wrappers
Some special wrappers exist for GCC. For example, "c89" is a wrapper (in the form of a script) that calls "gcc" and specifies that the C89 standard be used. Other wrappers include c99 (C99 standard), f95 (Fortran 95), and others. Wrappers serve as shortcuts. Instead of typing parameters or knowing the full name of a cross-compiler (like “arm-linux-androideabi-*”), users can type a command that is easier to remember.

Programmers could make their own wrappers if needed. To make a wrapper, create a simple shell script. For illustration, Android developers may make a wrapper for the Android cross-compiler for ARM devices to compile C++ code (arm-linux-androideabi-g++). In the script, a hashpling is needed (#!/bin/sh). Next, include this line - “arm-linux-androideabi-g++ HARD-CODED-PARAMS $@”. The programmer can include hard-coded parameters that are always needed like include parameters (-I) for Android libraries. The “$@” is a variable representing all of the parameters given to the wrapper. Lastly, save and name the script. A name like “droidc” would be suitable. Place the script in the system's executable path (like /usr/bin/ as listed in $PATH) and give the script executable permissions (chmod +x /usr/bin/droidc). To use the wrapper, type something like “droidc -c app.cpp”. Doing so is equivalent to typing “arm-linux-androideabi-g++ -O2 -I/usr/lib/my/android/libs -Wall -c app.cpp”. The wrapper can also include other commands if needed.

Cross-Compiling
To cross-compile using gcc, the developer can use "arm-linux-androideabi-gcc" instead of "gcc" to cross-compile for Android running on ARM. Alternately, add "--toolchain=arm-linux-androideabi" as a parameter to the "gcc" command to achieve the same results. Again, the developer could also type "--arch=arm" to cross-compile to ARM.

Programmers can also use Clang via gcc to cross-compile code. For instance, to cross-compile through Clang via gcc, the parameter is "--toolchain=arm-linux-androideabi-clang3.3" or "--arch=arm –llvm-version=3.3".

Interestingly, programmers can call Clang directly and use parameters to use gcc under Clang to compile the code - "-target armv5te-none-linux-androideabi -gcc-toolchain $NDK/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64".

MinGW (Minimalist GNU for Windows) is a cross-compiler that is used to make Windows applications. MinGW uses a port of the GNU compiler collection, thus MinGW supports C/C++, Objective C/C++, Ada, and Fortran. MinGW supports compiling 32 and 64-bit programs for Windows systems, as long as the system has the needed compilers and libraries. The MingGW compiler interfaces with the GNU Compiler Collection, thus allowing users to use similar commands and parameters to cross-compile the code for Windows as a developer would for compiling Linux programs. For example, to cross-compile C++ code with MinGW for an i586 Windows machine, try using the command below.

Code:
i586-mingw32msvc-g++ -c prog.cpp
i586-mingw32msvc-g++ -o Program.exe prog.o
# To disassemble:
i586-mingw32msvc-objdump -x Program.exe

For a 64-bit Windows machine, replace "i586-mingw32msvc" with "x86_64-w64-mingw32" and remember to change any parameters and libraries as needed.

On most systems, users can type "ls /usr" and see some of the directories containing cross-compilers. Remember, /usr/ is a directory that contains some common and essential directories like /usr/bin/ and /usr/share/. Not everything in /usr/ is a cross-compiler. The user is looking for directories that look like one of the following below. Keep in mind that other cross-compilers exist other than the listed below.
  • aarch64-linux-gnu
  • amd64-mingw32msvc
  • arm-linux-androideabi
  • arm-linux-gnueabi
  • arm-linux-gnueabihf
  • i586-mingw32msvc
  • i686-linux-android
  • i686-w64-mingw32
  • powerpc64le-linux-gnu
  • powerpc-linux-gnu
  • x86_64-linux-gnu
  • x86_64-w64-mingw32

For each of these cross-compilers, the common command-line usage look like this - "powerpc-linux-gnu-gcc PARAMS". If cross-compiling C++ to PowerPC, try "powerpc-linux-gnu-g++", or for Fortran "powerpc-linux-gnu-gfortran".

Transpiling
Below are some sample commands for transcompilation and other helpful tricks. The octothorpes/pound-signs (#) are comments, not code that is usable.

# C/C++ => LLVM Bytecode
# *.c/*.cpp => *.ll (LLVM IR file)
clang -S -emit-llvm FILE.c

# LLVM Bytecode => LLVM Assembly
# *.ll => *.s
llc FILE.ll

# Compile C/C++ via LLVM bytecode
clang -emit-llvm -o foo.bc -c foo.c
clang -o foo foo.bc

NOTE: A LLVM Bytecode file (*.bc) is the same as a LLVM Object file (*.o).

# LLVM Bytecode => C/C++
llc -march=c -o code.c code.ll # C
llc -march=c++ -o code.cpp code.ll # C++

# C/C++ => Javascript
# *.c/*.cpp => a.out.js
emcc FILE.cpp
# run "node a.out.js" to test the file

# Python script => Python Bytecode
python -m py_compile SCRIPT.py # Python2 .pyc
python -OO -m py_compile SCRIPT.py # Python2 .pyo
python3 -m py_compile SCRIPT.py # Python3 .pyc
python3 -OO -m py_compile SCRIPT.py # Python3 .pyo

# Python Script => C/C++
cython SCRIPT.py # Python2 to C
cython --cplus SCRIPT.py # Python2 to C++
cython -3 SCRIPT.py # Python3 to C
cython -3 --cplus SCRIPT.py # Python3 to C++

# Python to Stand-alone binary executable
cxfreeze SCRIPT.py # Python2
cxfreeze3 SCRIPT.py # Python3
cxfreeze3 -OO SCRIPT.py # Python3 Optimized

NOTE: Some of you may be wondering why the above example is using cxfreeze3 (which is for Python3 scripts) despite the fact that most (or all) repos lack cxfreeze3. A cxfreeze version for Python3 has been released, but the source code does not compile correctly. I got the source code and fixed the bug. I now have a perfectly working copy of cxfreeze3 that produces Python3-based stand-alone executables that execute very well. If you would like cxfreeze3 on your system, read the directions near the end of this article. If needed, email me and I will send you the fix for the bug and directions for installation.

Recommended Tutorials
For those of you wanting to learn how to use a compiler mentioned in this article, checkout these links. However, this is not a complete list.

CxFreeze for Python3
This section just pertains to developers wanting cxfreeze for Python3. Most readers can skip this section.

Running "pip install cx_Freeze" in a command-line installs cxfreeze for Python2. Using "pip3" instead of "pip" will show an error with setting up cxfreeze for Python3. However, cxfreeze can still be obtained. I have tested these directions successfully using cxfreeze 4.3.3.

1. Download the source code from one of these two websites - https://pypi.python.org/pypi/cx_Freeze | http://sourceforge.net/projects/cx-freeze/files/
2. View the code for the "setup.py"
3. Go to line 80
4. Depending on the version of cxfreeze obtained, the line "if not vars.get("Py_ENABLE_SHARED", 0):" will be line number 80 or 81 (possibly even somewhere between 76-85).
HINT: The line before it is "vars = distutils.sysconfig.get_config_vars()".
5. Once found, replace "if not vars.get("Py_ENABLE_SHARED", 0):" with "if True:". Do not change the indent from the original layout.
6. Save and close the file.
7. Continue to install cxfreeze using the setup.py file as normal.
8. To use cxfreeze3 from a command-line, create a file named "cxfreeze3" and put in the code below (it is just three lines of code). Remember to save and close the file.

Code:
#!/usr/bin/env python3
from cx_Freeze import main
main()

9. Make the new script executable (IN-THE-SHELL:$ chmod +x cxfreeze3).
10. Place the script in /usr/bin/ or some other preferred path location using Root privileges (SHELL:$ cp cxfreeze3 /usr/bin/cxfreeze3).
 

Attachments

  • slide.jpg
    slide.jpg
    42.1 KB · Views: 130,322
  • cxfreeze3-PATCHED.zip
    567.7 KB · Views: 1,076
Last edited:


By the way ICC, intels very expensive C/C++ compiler, is free for Linux users in non-comercial settings. :)
 
Alright now that I have read this. Whats the absolute best way for some one who has zero to minimal skills install gnu/gcc? I'm looking at it right and since I don't know jack on the language side of things I have decided rebuilding my motor is gonna be less of a headache lol. Any tips tricks or pointers for one of the new guys on the block???? :D:D:D:D:D:D
 
Hmmm, I was surprised that it's not already installed in Ubuntu! So, anyway, this should do it:
Code:
sudo apt install gcc

After installing, you can determine where the binary is stored with:
Code:
which gcc  #probably /usr/bin/gcc

And you can confirm what version with:
Code:
gcc --version  #probably 7.3.0

Cheers
 
Now I just feel like an ass lol, that is the same way I installed Atom IO. The gnu website didn't make it seem like i could do that. I'm guessing it was showing the way to do it if you know how to do it manually.
 
Heck, I wasn't sure either... I just tried the standard install steps when I found it wasn't there already. It does come with Linux Mint, and many distros. There are so many programming environments and tools available that it is kind of staggering.... and I'm not even a programmer! :D:D
 
Heck, I wasn't sure either... I just tried the standard install steps when I found it wasn't there already. It does come with Linux Mint, and many distros. There are so many programming environments and tools available that it is kind of staggering.... and I'm not even a programmer! :D:D
Don't worry I'm no where near close. But I want to be, I just have the unique problem of being able to learn from paper sources. I'm a do it to learn it kind of person so its a bit of learning curve over here for me. I can build a computer, car, house, and many other things but programming is whole different beast. what is the Remmina that comes downloaded? And I'm guessing the text editor is the compiler previously mentioned?
 
Remmina is a remote desktop client for Linux. I haven't used it... I like an app called TeamViewer for that purpose. I can't converse very well about programming... really. I would call gcc the "compiler" in a generic sense, but there may be much more to it. The initials stand for GNU Compiler Collection, if that helps. You can write code in any text editor, as far as I know, but some people are very, very particular about it... be careful or you might start another war between the VIM and Emacs people! :eek::confused::D
 
Sadly I've started many wars that way but only on the book of faces and the car pages I follow. I plan on experimenting with both sides of the coin. more than likely I'll end up using both and cant find anything on the compiler? All I'm finding is the text editor. hhmmmmm......o_O:confused:
 
Those aren't the only ones... but those two have a very religious following! :eek::D You have many options for text/code editors, some with powerful options, some with less.

Not sure what you mean by "cant find anything on the compiler." What are you looking for?
 
Now I just feel like an ass lol, that is the same way I installed Atom IO. The gnu website didn't make it seem like i could do that. I'm guessing it was showing the way to do it if you know how to do it manually.
Don't worry I'm no where near close. But I want to be, I just have the unique problem of being able to learn from paper sources. I'm a do it to learn it kind of person so its a bit of learning curve over here for me. I can build a computer, car, house, and many other things but programming is whole different beast. what is the Remmina that comes downloaded? And I'm guessing the text editor is the compiler previously mentioned?
Sadly I've started many wars that way but only on the book of faces and the car pages I follow. I plan on experimenting with both sides of the coin. more than likely I'll end up using both and cant find anything on the compiler? All I'm finding is the text editor. hhmmmmm......o_O:confused:

OK, so I'm getting that there is some confusion between a compiler and a text editor.
Atom.io is a text editor, used to edit text.
gcc is GNU's C compiler used to compile code into an executable binary.

From the previous posts, I'm assuming that you have managed to install gcc.
If you are running a debian based distro, I'd also recommend installing the build-essential meta-package.
Code:
sudo apt install build-essential
That will install a few extra packages that will make it easier to build software from source.

re: "... and can't find anything on the compiler? All I'm finding is the text editor."

If you mean you are in atom.io and you can't see anything that refers to a compiler, that is probably because you have atom.io set up purely as a text editor and NOT as an IDE (Integrated Development Environment - which allows you to write, build and test code.).

I'm not an atom.io user, so I don't know a huge amount about it, but I think there are some extra packages that you need to install for atom.io that will turn it into an IDE.

Take a look at this page:
https://atom.io/packages/atom-ide-ui

It says to install the atom-ide-ui package from "install" in atoms settings, or to use the command:
Code:
apm install atom-ide-ui

That should change atom.io's interface to look more like an IDE.

However, it looks like you also need to install additional packages to add support for syntax highlighting for the programming languages you are interested in and to add support for debugging.

So if you are interested in learning C/C++ it looks like you would need to install the language-c package and the atom-ide-debugger-native-gdb package to install support for gdb.
But it doesn't look as if there is a way to actually build C or C++ files from inside atom.io ATM. Which seems a bit odd!

Wow, that all sounds a bit nuts.
Instead of atom.io, maybe you'd be better off installing an established, dedicated IDE like Codeblocks, QTCreator, or KDevelop!

Personally, I do all of my development using Vim in the terminal. And if I ever want an IDE type layout, I'll run vim in tmux (a terminal multiplexer) with an extra pane containing a terminal for building/debugging.... But that's just me.

Screenshot below!:
shot.png
 
@JasKinasis Thank you for your input on the subject. I have tried downloading the c package for atom.io but its buggy for the most part and doesn't seem to take input too well. I will definitely be taking your advice and downloading the ones you recomended and figuring out which one I like best. I work graveyards doing freight so my days are always out of wack. And I just downloaded the build essential package you mentioned. I'm slowly but surely getting aquanted to linux and ubuntu with the help of some apps I found on google play for my phone. any other tips, tricks, reccomendations, etc. are greatly appreciated.

Edit: this is the error that is thrown with in the script for the C/C++ compiler package for atom.io if I need to move this to a different thread or create one for this I will. Any input would be greatly appreciated.
Thanks in advance from over there.
https://github.com/kriscross07/atom-gpp-compiler/issues/134
 
Last edited:

Members online

No members online now.

Top